Java XML 读取器错误
当我运行 XML 阅读器的开头时,出现错误:
public static void main(String[] args)
{
System.out.println("XML Reader");
try
{
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
Document doc = docBuilder.parse("database.xml");
//normalize text
doc.getDocumentElement().normalize();
System.out.println("The root of this doc is "+doc.getDocumentElement().getNodeName());
NodeList listOfAddresses = doc.getElementsByTagName("address");
int totalAddresses = listOfAddresses.getLength();
System.out.println(totalAddresses+" addresses in "+ doc.getDocumentElement().getNodeName());
//main loop
for(int i = 0; i<listOfAddresses.getLength(); i++)
{
Node items = listOfAddresses.item(i);
if(items.getNodeType() == Node.ELEMENT_NODE)
{
System.out.println("Address #"+i);
Element element = (Element)items;
NodeList nameList = element.getElementsByTagName("name");
Element nameElement = (Element)nameList.item(0);
NodeList nameOutput = nameElement.getChildNodes();
System.out.println("name: "+nameElement);
}
}
}
catch(SAXParseException err)
{
System.out.println("Sax Parse Exception error on line "+err.getLineNumber());
}
catch(SAXException e)
{
System.out.println("SAX Exception error");
Exception x = e.getException();
((x == null) ? e : x).printStackTrace();
}
catch(Throwable t)
{
System.out.println("Trowable error");
t.printStackTrace();
}
}
netbeans 给出以下输出:
run: XML Reader The root of this doc is database 2 addresses in database java.lang.NullPointerException Address #0 Trowable error at xmlreader.XMLreader.main(XMLreader.java:42)
有人能帮我解决这个问题吗?
I get an error when i run the beginning of my XML reader:
public static void main(String[] args)
{
System.out.println("XML Reader");
try
{
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
Document doc = docBuilder.parse("database.xml");
//normalize text
doc.getDocumentElement().normalize();
System.out.println("The root of this doc is "+doc.getDocumentElement().getNodeName());
NodeList listOfAddresses = doc.getElementsByTagName("address");
int totalAddresses = listOfAddresses.getLength();
System.out.println(totalAddresses+" addresses in "+ doc.getDocumentElement().getNodeName());
//main loop
for(int i = 0; i<listOfAddresses.getLength(); i++)
{
Node items = listOfAddresses.item(i);
if(items.getNodeType() == Node.ELEMENT_NODE)
{
System.out.println("Address #"+i);
Element element = (Element)items;
NodeList nameList = element.getElementsByTagName("name");
Element nameElement = (Element)nameList.item(0);
NodeList nameOutput = nameElement.getChildNodes();
System.out.println("name: "+nameElement);
}
}
}
catch(SAXParseException err)
{
System.out.println("Sax Parse Exception error on line "+err.getLineNumber());
}
catch(SAXException e)
{
System.out.println("SAX Exception error");
Exception x = e.getException();
((x == null) ? e : x).printStackTrace();
}
catch(Throwable t)
{
System.out.println("Trowable error");
t.printStackTrace();
}
}
netbeans is giving me the following output:
run: XML Reader The root of this doc is database 2 addresses in database java.lang.NullPointerException Address #0 Trowable error at xmlreader.XMLreader.main(XMLreader.java:42)
Could someone help me figure this one out?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您没有检查是否存在元素 0。本质上,如果索引无效,.item() 将返回 null,因此可能不存在索引为 0 的项目返回 null,然后您尝试调用 getChildNodes () 位于空指针上。因此你的 NPE。
您应该迭代节点列表中的元素。
更改
为
You're not checking if there IS an element 0. Essentially, .item() will return null if the index isn't valid, as such there may not be an item with index 0 which returns null and then you try to call getChildNodes() on the null pointer. Hence your NPE.
You should be iterating over the elements in the node list.
change
to