0

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.

JamEngulfer
  • 747
  • 4
  • 11
  • 33

2 Answers2

1

Try getTextContent() instead of getNodeName(). See if that helps.

getNodeName() simply returns a String according to the type of the node, not its contents.

EDIT

Try

while (child != null && !child.getNodeName().equals("latest")) {
    child = child.getNextSibling();
}

EDIT

All of the above didn't work.

I think the real problem is here:

Node pluginsNode = doc.getFirstChild();

Try replacing this with:

Node pluginsNode = (Node)doc.getDocumentElement();

According to this question.

EDIT

After some debugging here's the solution:

private double extractVersion(Document doc){

String result = "";

NodeList nodeList = doc.getElementsByTagName("latest");
for (int i = 0; i < nodeList.getLength(); i++) {
    Node node = nodeList.item(i);
    if (node.getNodeName().equals("latest")) {
        result = node.getTextContent();
    }
}

return Double.parseDouble(result);
}
Community
  • 1
  • 1
Octoshape
  • 1,131
  • 8
  • 26
  • Nope. Still got a NPE at the same spot. – JamEngulfer Nov 25 '13 at 18:53
  • What type is `PLUGIN_NAME`? Can you ensure with the debugger that `child` is really the correct `Node` before you enter the `while` loop? – Octoshape Nov 25 '13 at 19:02
  • `PLUGIN_NAME` is a String. By my count it `child` should indeed be the correct `Node`. I used your EDIT code and now it's returning a NPE on the line `String latest = child.getFirstChild().getNodeValue();` I tried playing around with that and nothing appeared to work. – JamEngulfer Nov 25 '13 at 19:13
  • Unfortunately, that didn't stop the line `String latest = child.getFirstChild().getNodeValue();` from returning a NPE – JamEngulfer Nov 25 '13 at 19:27
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/41895/discussion-between-octoshape-and-jamengulfer221) – Octoshape Nov 25 '13 at 19:30
0

I suppose it happend when you reach last node and there is simply no next sibiling. According to documentation http://docs.oracle.com/javase/1.5.0/docs/api/org/w3c/dom/Node.html#getNextSibling() getNextSibiling and getFirstSibiling may return null. You should check if child returned from this call is not null