英文:
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 :
<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>
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 <fff xlink=""/>
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("/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");
}
}
}
答案1
得分: 2
问题在于
<fffxlink=""/>
没有子元素,因此它也没有第一个子元素,所以
child.getFirstChild()
因此
child.getFirstChild().getNodeValue()
会抛出 NPE。
解决方法:
if ((child.getFirstChild() != null) && (child.getFirstChild().getNodeValue() != null))
英文:
The problem is that
<fffxlink=""/>
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) && (child.getFirstChild().getNodeValue() != null))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论