我試圖從這裏賈烏德我的XML文件中獲取的所有作者是XML代碼空指針在Java中
<?xml version="1.0"?>
<map>
<authors>
<author>testasdas</author>
<author>Test</author>
</authors>
</map>
下面是我在Java中使用
代碼public static List<String> getAuthors(Document doc) throws Exception {
List<String> authors = new ArrayList<String>();
Element ed = doc.getDocumentElement();
if (notExists(ed, "authors")) throw new Exception("No authors found");
Node coreNode = doc.getElementsByTagName("authors").item(0);
if (coreNode.getNodeType() == Node.ELEMENT_NODE) {
Element coreElement = (Element) coreNode;
NodeList cores = coreElement.getChildNodes();
for (int i = 0; i < cores.getLength(); i++) {
Node node = cores.item(i);
if (node.getNodeType() == Node.ELEMENT_NODE) {
Element e = (Element) node;
String author = e.getElementsByTagName("author").item(i).getTextContent();
Bukkit.getServer().broadcastMessage("here");
authors.add(author);
}
}
}
return authors;
}
我在嘗試運行代碼時遇到了java.lang.NullPointerException
錯誤,但我不知道爲什麼。
09.04 17點05分24秒[服務器]重症在com.dcsoft.arenagames.map.XMLHandler.getMapData(XMLHandler.java:42)
09.04 17點05分24秒[服務器]重症在COM。 dcsoft.arenagames.map.XMLHandler.getAuthors(XMLHandler.java:73)
09.04 17時05分24秒[服務器]重症顯示java.lang.NullPointerException
完整的堆棧跟蹤在哪裏? – BobTheBuilder 2013-04-09 16:17:11
'嘗試.. catch' - >堆棧跟蹤? – 2013-04-09 16:17:12
這是XMLHandler.java的第73行嗎? – 2013-04-09 16:20:52