字符串比较时空指针异常
来源:互联网 发布:hbuilder mui商城源码 编辑:程序博客网 时间:2024/06/10 03:39
当我们在写代码时,经常会犯如下的错误:
例如,我们要解析一个XML文档,
book.xml 如下所示:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<书架>
<书>
<书名 >java</书名>
<作者>作者1</作者>
<售价>234242</售价>
</书>
<书>
<书名>java web</书名>
<作者>作者2</作者>
<售价>120</售价>
</书>
</书架>
我们利用junit进行单元测试
代码如下为使用SAX进行XML 解析,并且将解析出来的一个个书的对象,封装到Book对象当中,形成一个List集合返回
首先,Book的javabean实现如下:主要是封装书的信息
package com.sax;
public class Book {
private String bookName;
private String author;
private double price;
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}
SAX解析如下:
package com.sax;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.junit.Test;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;
public class Demo3 {
/**
* @throws ParserConfigurationException
* @throws SAXException
* @throws IOException
*/
@Test
public void test() throws ParserConfigurationException, SAXException, IOException {
// 1.创建解析工厂
SAXParserFactory factory = SAXParserFactory.newInstance();
// 2.得到解析器
SAXParser sp = factory.newSAXParser();
// 3.得到读取器
XMLReader reader = sp.getXMLReader();
// 4.设置内容读取器
BeanListHandler handler=new BeanListHandler();
reader.setContentHandler(handler);
// 5.读取XML文档内容
reader.parse("src/book.xml");
List<Book> list=handler.getList();
System.out.println(list);
}
}
//封装成对象 返回
class BeanListHandler extends DefaultHandler{
private List<Book> list=new ArrayList<Book>();
private String currentTag;
private Book book;
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
currentTag=qName;
if("书".equals(currentTag)){
book=new Book();
}
}
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
if("书名".equals(currentTag)){
book.setBookName(new String(ch,start,length));
}
if("作者".equals(currentTag)){
book.setAuthor((new String(ch,start,length)));
}
if("售价".equals(currentTag)){
book.setPrice(Double.parseDouble((new String(ch,start,length))));
}
}
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
if("书".equals(qName)){
list.add(book);
book=null;//置空
}
currentTag=null;
}
public List<Book> getList() {
return list;
}
}
我们常常犯的错误是把:
if("书名".equals(currentTag)){
book.setBookName(new String(ch,start,length));
}
if("作者".equals(currentTag)){
book.setAuthor((new String(ch,start,length)));
}
if("售价".equals(currentTag)){
book.setPrice(Double.parseDouble((new String(ch,start,length))));
}
写成:
if(currentTag.equals("书名")){
book.setBookName(new String(ch,start,length));
}
if(currentTag.equals("作者")){
book.setAuthor((new String(ch,start,length)));
}
if(currentTag.equals("售价")){
book.setPrice(Double.parseDouble((new String(ch,start,length))));
}
会报空指针异常
因为当读取到XML文档的结束标签的时候,会设置currentTag为空,所以此时,会读取</书名>之后的空白也就是</书名> <作者>之间的空白部分,因此会执行,characters函数,从而执行if(currentTag.equals("书名")){
book.setBookName(new String(ch,start,length));
}
也就是说判断 null.equals("书名")因此会报错。
- 字符串比较时空指针异常
- RecyclerView 使用时空指针异常
- 点击Layout跳转时空指针异常
- mybatis下opening session时空指针异常
- spring配置好后,写入数据时空指针异常,objectFactory
- quartz整合Spring注入Service时空指针异常问题解决
- quartz整合Spring注入Service时空指针异常问题解决
- 关于android在fragment中调用getActivity()功能进行Toast或者setAdapter时空指针的异常
- eclipse中,junit对单个方法进行测试时空指针异常(java.lang.NullPointerException)
- 指针实现strcmp字符串比较
- AdbChimpDevice创建时空指针错误
- 指针做形参比较字符串的大小、
- 字符串比较函数的指针错误
- 指针实现字符串比较函数strcmp
- 安卓调用activity时空指针错误
- listvuew测量view高度时空指针问题
- 字符串指针 与 字符串比较的正确方法
- 用指针(比较三个字符串的大小)
- const和define的区别
- 自动化测试
- 删除kafka 残余topics
- python 解压文件
- 多线程的设计模式
- 字符串比较时空指针异常
- Android Studio 创建assets 文件夹
- spring各个jar包的作用
- 二三层转发具体过程
- SSO单点登录三种情况的实现方式详解
- Mac下配置ndk/adb
- Mac下安装Ruby环境
- SqlServer远程连接
- 线程类Thread的API接口分析系列之读写锁ReentrantReadeWriteLock