I'm trying to read this XML file from a URL:
<updates>
<plugin name="PluginName">
<latest>0.7</latest>
<url>[PLUGIN URL]</url>
<notes>
[UPDATE NOTES]
</notes>
<message/>
</plugin>
</updates>
This is my Java code to read the document:
private Document getXML(){
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setIgnoringElementContentWhitespace(true);
Document doc = null;
try {
DocumentBuilder db = dbf.newDocumentBuilder();
try {
doc = db.parse(new URL(XML_URL).openStream());
System.out.println("Successfully read XML from URL");
return doc;
} catch (MalformedURLException e) {
log(Level.SEVERE, "Update URL was borked");
e.printStackTrace();
} catch (SAXException e) {
log(Level.SEVERE, "I don't even know what happened here");
e.printStackTrace();
} catch (IOException e) {
log(Level.SEVERE, "Something in your connection broke");
e.printStackTrace();
}
} catch (ParserConfigurationException e) {
log(Level.SEVERE, "Unable to create Parsing Document, complain to developers");
e.printStackTrace();
}
return doc;
}
The Document object returned by this method is then passed to this method that parses it:
private double extractVersion(Document doc){
Node pluginsNode = doc.getFirstChild();
Node pluginNode = pluginsNode.getFirstChild();
while(pluginNode.hasAttributes() && !pluginNode.getAttributes().getNamedItem("name").equals(PLUGIN_NAME)){
pluginNode = pluginNode.getNextSibling();
}
Node child = pluginNode.getFirstChild();
System.out.println("Child: "+child);
System.out.println("Pnode:" + pluginNode);
while (!child.getNodeName().equals("latest")){
child = child.getNextSibling();
if(child == null){
System.out.println("SOMETHING HAPPENED");
}
}
String latest = child.getFirstChild().getNodeValue();
return Double.parseDouble(latest);
}
I end up getting a null pointer exception from this line whenever I run the code:
while (!child.getNodeName().equals("latest")){
I've changed stuff for hours and tried to get help elsewhere but I can't figure out what's going on and why I get a null pointer exception.