Java中 child.getFirstChild().getNodeValue() 会导致空指针异常。

huangapple go评论72阅读模式
英文:

Java child.getFirstChild().getNodeValue() null pointer

问题

我有一个像这样的XML文件:

<root>
    <item1>
        <object1>123</object1>
        <object2>321</object2>
        <fffxlink=""/>
    </item1>
    <item2>
        <object1>fsddf</object1>
        <object2>dsf</object2>
        <fff xlink=""/>
    </item2>
    <item3>
        <object1>7867</object1>
        <object2>87687</object2>
    </item3>
    <fff xlink=""/>
</root>

我尝试获取所有item和object的名称以及每个object的值。问题是我的代码尝试获取`<fff xlink=""/>`的值,我尝试使用`child.getFirstChild().getNodeValue() != null`来忽略它的值,但我始终得到java空指针异常。

XPathFactory factory = XPathFactory.newInstance();
XPath xpath = factory.newXPath();

XPathExpression expr = xpath.compile("/item/object/*");
Object result = expr.evaluate(document, XPathConstants.NODESET);
NodeList nodes = (NodeList) result;
System.out.println("Nb objets: " + nodes.getLength());
for (int i = 0; i < nodes.getLength(); i++) {
    Element el = (Element) nodes.item(i);
    System.out.println("item: " + el.getNodeName());
    NodeList children = el.getChildNodes();
    for (int k = 0; k < children.getLength(); k++) {
        Node child = children.item(k);
        if (child.getNodeType() != Node.TEXT_NODE) {
            System.out.println("object: " + child.getNodeName());
            if (child.getFirstChild().getNodeValue() != null) {
                if (child.getFirstChild().getNodeType() == Node.TEXT_NODE)
                    System.out.println("Value: " + child.getFirstChild().getNodeValue());
            } else
                System.out.println("null");
        }
    }
}
英文:

I have an XML file like this :

    &lt;root&gt;
&lt;item1&gt;
&lt;object1&gt;123&lt;object1&gt;
&lt;object2&gt;321&lt;/object2&gt;
&lt;fffxlink=&quot;&quot;/&gt;
&lt;/item1&gt;
&lt;item2&gt;
&lt;object1&gt;fsddf&lt;/object1&gt;
&lt;object2&gt;dsf&lt;/object2&gt;
&lt;fff xlink=&quot;&quot;/&gt;
&lt;/item2&gt;
&lt;item3&gt;
&lt;object1&gt;7867&lt;/object1&gt;
&lt;object2&gt;87687&lt;/object2&gt;
&lt;/item3&gt;
&lt;fff xlink=&quot;&quot;/&gt;
&lt;/root&gt;

I try to get all item ands object names with value of each object. The problem is that my code tries to get value of &lt;fff xlink=&quot;&quot;/&gt; and I tried this child.getFirstChild().getNodeValue() != null to ignore his value but I get always java null pointer exception

        XPathFactory factory = XPathFactory.newInstance();
XPath xpath = factory.newXPath();
XPathExpression expr = xpath.compile(&quot;/item/object/*&quot;);
Object result = expr.evaluate(document, XPathConstants.NODESET);
NodeList nodes = (NodeList) result;
System.out.println(&quot;Nb objets : &quot; + nodes.getLength());
for (int i = 0; i &lt; nodes.getLength(); i++) {
Element el = (Element) nodes.item(i);
System.out.println(&quot;item : &quot; + el.getNodeName());
NodeList children = el.getChildNodes();
for (int k = 0; k &lt; children.getLength(); k++) {
Node child = children.item(k);
if (child.getNodeType() != Node.TEXT_NODE) {
System.out.println(&quot;object : &quot; + child.getNodeName());
if (child.getFirstChild().getNodeValue() != null)
{
if (child.getFirstChild().getNodeType() == Node.TEXT_NODE) System.out.println(&quot;Value :&quot; + child.getFirstChild().getNodeValue());
}
else System.out.println(&quot;null&quot;);
}
}
}

答案1

得分: 2

问题在于

&lt;fffxlink=&quot;&quot;/&gt;

没有子元素,因此它也没有第一个子元素,所以

child.getFirstChild()

因此

child.getFirstChild().getNodeValue()

会抛出 NPE。

解决方法:

if ((child.getFirstChild() != null) &amp;&amp; (child.getFirstChild().getNodeValue() != null))
英文:

The problem is that

&lt;fffxlink=&quot;&quot;/&gt;

has no children, so it has no first child either, so

child.getFirstChild()

hence

child.getFirstChild().getNodeValue()

will throw a NPE.

Solution:

if ((child.getFirstChild() != null) &amp;&amp; (child.getFirstChild().getNodeValue() != null))

huangapple
  • 本文由 发表于 2020年9月13日 08:27:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/63866065.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定