简体   繁体   中英

Java DOM - NULL pointer exception

I am parsing an XML document using DOM in Java. The data looks like this:

<nodes totalCount="48" count="10">
  <node type="A" id="83" label="label1">
    <record>new</record>
    <createTime>12345</createTime>
  </node>
  <node type="A" id="77" label="label2">
    <record>new</record>
    <createTime>4567</createTime>
  </node>
</nodes>

This is the relevant portion of my code that I am using to parse:

Document doc =  dBuilder.parse(new InputSource(reader));
doc.getDocumentElement().normalize();
Log.w(TAG, "Dom Root element :" + doc.getDocumentElement().getNodeName());
NodeList nList = doc.getElementsByTagName("node");

for (int temp = 0; temp < nList.getLength();temp++) {
        Element element = (Element) nList.item(temp);
        NodeList time = element.getElementsByTagName("createTime");
        Element line = (Element) time.item(0);
        String value =getDataFromElement(line);  
        Log.w(TAG, "Create time: " + value );
 }

And my getDataFromElement method is

public static String getDataFromElement(Element e) {
        Node child = e.getFirstChild();
        if (child instanceof CharacterData) {
          CharacterData cd = (CharacterData) child;
          return cd.getData();
        }
   return "";
}

My problem is, it gives null pointer exception after printing the first value of createTime at

getDataFromElement() method

Can anyone help me diagnose this issue?

It happens when any "node"-element has no "createTime"-element

Use try/catch to eliminate unwanted nodes:

jcomeau@intrepid:/tmp$ cat so.xml ElementTest.java; java ElementTest
<?xml version="1.0" encoding="UTF-8"?>
<nodes totalCount="48" count="10">
<node type="A" id="83" label="label1">
<record>new</record>
<createTime>12345</createTime>
<node type="B">
</node>
</node>
<node type="A" id="77" label="label2">
<record>new</record>
<createTime>4567</createTime>
</node>
</nodes>
import org.w3c.dom.*;
import javax.xml.parsers.*;
import java.io.*;
import org.xml.sax.*;
public class ElementTest {
 public static void main(String args[]) throws Exception {
  InputStream reader = ElementTest.class.getResourceAsStream("so.xml");
  DocumentBuilder dBuilder = DocumentBuilderFactory.newInstance(
   ).newDocumentBuilder();
  Document doc = dBuilder.parse(new InputSource(reader));
  doc.getDocumentElement().normalize();
  System.out.println("Dom Root element :" + doc.getDocumentElement().getNodeName());
  NodeList nList = doc.getElementsByTagName("node");
  for (int temp = 0; temp < nList.getLength();temp++) {
   Element element = (Element) nList.item(temp);
   NodeList time = element.getElementsByTagName("createTime");
   Element line = (Element) time.item(0);
   try {
    String value =getDataFromElement(line);  
    System.out.println("Create time: " + value );
   } catch (Exception problem) {
    System.err.println("problem element " + line + ": " + problem);
   }
  }
 }
 public static String getDataFromElement(Element e) {
  Node child = e.getFirstChild();
  if (child instanceof CharacterData) {
   CharacterData cd = (CharacterData) child;
   return cd.getData();
  }
  else return "";
 }
}
Dom Root element :nodes
Create time: 12345
problem element null: java.lang.NullPointerException
Create time: 4567

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM