字符串比较时空指针异常

    xiaoxiao2021-03-25  141

    当我们在写代码时,经常会犯如下的错误:

    例如,我们要解析一个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("书名")因此会报错。

    转载请注明原文地址: https://ju.6miu.com/read-9711.html

    最新回复(0)