问答详情
源自:3-4 使用 SAX 解析将 XML 的内容和结构存入 JAVA 对象

输出BookList的时候空指针异常了~~~


for(Book book : handler.getBookList()){

System.out.println(book.getId() + " ");

System.out.println(book.getAuthor() + " ");

System.out.println(book.getName() + " " + book.getLanguage() + " ");

System.out.println(book.getPrice() + " " + book.getYear() + " ");

System.out.println("-----finish-----");

}

//handler内容

public void endElement(String uri, String localName, String qName)

throws SAXException {

// TODO Auto-generated method stub

super.endElement(uri, localName, qName);

if(qName.equals("book")){

book = null;

System.out.println("本书完");

}else if(qName.equals("name")){

book.setName(value);

}else if(qName.equals("author")){

book.setAuthor(value);

}else if(qName.equals("year")){

book.setYear(value);

}else if(qName.equals("price")){

book.setPrice(value);

}else if(qName.equals("language")){

book.setLanguage(value);

}

}

//结果

Exception in thread "main" java.lang.NullPointerException

at com.imooc.test.SAXtest.main(SAXtest.java:27)


提问者:黯夜旋风 2015-12-21 22:46

个回答

  • 静静的闹铃
    2015-12-22 00:03:26

    http://img.mukewang.com/567822bf000130fd03560150.jpg看图,你少了一句