Java DOM - NULL 指针异常

发布于 2024-11-25 19:00:15 字数 1437 浏览 1 评论 0原文

我正在使用 Java 中的 DOM 解析 XML 文档。数据如下所示:

<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>

这是我用来解析的代码的相关部分:

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 );
 }

我的 getDataFromElement 方法是

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

我的问题是,它在之后给出了空指针异常 中打印 createTime 的第一个值

getDataFromElement() 方法

谁能帮我诊断这个问题?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

混浊又暗下来 2024-12-02 19:00:15

当任何“node”元素没有“createTime”元素时,就会发生这种情况

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

ぽ尐不点ル 2024-12-02 19:00:15

使用 try/catch 消除不需要的节点:

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文