我正在尝试从我在 java 中的 xml 文件中获取所有作者这里是 xml 代码
<?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