写Android时总是遇到空指针问题,主要的原因是因为好多时候类没有初始化的缘故。
public class XMLParserHandler extends DefaultHandler {
private Book book=new Book();
private ArrayList<Book> booklist=new ArrayList<Book>();
private String nodeName;
private StringBuilder value;
@Override
public void startDocument() throws SAXException {
// TODO Auto-generated method stub
super.startDocument();
value=new StringBuilder();
}


@Override
public void endDocument() throws SAXException {
// TODO Auto-generated method stub
super.endDocument();
}


@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
// TODO Auto-generated method stub
super.startElement(uri, localName, qName, attributes);
nodeName=localName;
value.setLength(0);
}


@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
// TODO Auto-generated method stub
super.endElement(uri, localName, qName);
if(qName.equals("data")){
booklist.add(book);
}
}
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
// TODO Auto-generated method stub
super.characters(ch, start, length);
value.append(ch,start,length);
Log.d("mainactivity",value.toString().trim());

if(nodeName.equals("id")){
book.setId(Integer.parseInt(value.toString().trim()));
}else if(nodeName.equals("study_number")){
book.setStudy_number(Integer.parseInt(value.toString().trim()));
}else if(nodeName.equals("name")){
book.setName(value.toString().trim());
}else if(nodeName.equals("sex")){
book.setSex(value.toString().trim());
}else if(nodeName.equals("major")){
book.setMajor(value.toString().trim());
}else if(nodeName.equals("enroll_year")){
book.setEnroll_year(Integer.parseInt(value.toString().trim()));
}else if(nodeName.equals("class")){
book.setClassw(value.toString().trim());
}else if(nodeName.equals("telephone")){
book.setTelephone(value.toString().trim());
}else if(nodeName.equals("qq")){
book.setQq(value.toString().trim());
}else if(nodeName.equals("email")){
book.setEmail(value.toString().trim());
}else if(nodeName.equals("remark")){
book.setRemark(value.toString().trim());
}
}


public ArrayList<Book> getBooklist() {
return booklist;
}


public void setBooklist(ArrayList<Book> booklist) {
this.booklist = booklist;
}