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.

8
  • What do the various methods you use return? getNodeName(), getFirstChild()? Commented Nov 25, 2013 at 18:24
  • What is null? child or what is returned by getFirstChild()? Commented Nov 25, 2013 at 18:24
  • 1
    What debugging have you done? Have you confirmed that child.getNextSibling() returns a valid reference or that it returns null? Commented Nov 25, 2013 at 18:28
  • Is there a way I can just get the text value of a node to print out? If I just print any element or part of the document, it just returns null Commented Nov 25, 2013 at 18:37
  • This kind of DOM-parsing would be easier working on the elements only. Use document.getDocumentElement() as a starting point Commented Nov 25, 2013 at 18:38

2 Answers 2

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);
}
Sign up to request clarification or add additional context in comments.

5 Comments

Nope. Still got a NPE at the same spot.
What type is PLUGIN_NAME? Can you ensure with the debugger that child is really the correct Node before you enter the while loop?
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.
Unfortunately, that didn't stop the line String latest = child.getFirstChild().getNodeValue(); from returning a NPE
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.