0

I have a class named xmlReader that have parse(String path) and parseXml(Document doc) methods. I define:

    xmlReader reader = new xmlReader();
    Document doc =   reader.parse(PATH);
    reader.parseXml(doc);`

My parseXml method:

public void parseXml(Document doc)
{
    Node first = doc.getFirstChild().getFirstChild();
    NamedNodeMap att = first.getAttributes();
    Node id = att.item(0);
    NodeList l = first.getChildNodes();
    System.out.println("id:" + id.getNodeValue());
    for(int i = 0; i < l.getLength(); i++)
    {
        Node temp = l.item(i);
        System.out.println(temp.getNodeName() + ": " +
                temp.getNodeValue());
    }

}

The problem: line 3 of parseXml method:

When Node id = att.item(0) the program get a null ref exception. When debugging I see that the doc is defined null. Why is that? Its like its not reading the file correctly.

Thanks?

This is my parse(String path) method:

public Document parse(String path) 
{
DocumentBuilderFactory dbf =
DocumentBuilderFactory.newInstance();
DocumentBuilder db = null;

try 
{
    db = dbf.newDocumentBuilder();
} catch (ParserConfigurationException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
Document doc = null;
try 
{
    doc = db.parse(path);
} catch (SAXException e) {

    e.printStackTrace();
} catch (IOException e) {

    e.printStackTrace();
}
return doc;
}
7
  • 3
    "It's like its not reading the file correctly." Ummm, perhaps it's not reading the file correctly. How do you know that's not the problem? Commented Jan 9, 2012 at 22:08
  • 3
    are you sure PATH is correct? Commented Jan 9, 2012 at 22:09
  • If doc is null then indeed the problem is with doc = reader.parse(PATH); not with your method. Commented Jan 9, 2012 at 22:15
  • There is no way that code will run to att.item(0) if the doc variable is null. Are you sure that is the same "doc" or is it maybe something you found in the objects making up the DOM tree? Commented Jan 9, 2012 at 22:16
  • 3
    Could you please provide us with the stacktrace of the exception? Commented Jan 9, 2012 at 22:18

1 Answer 1

3

Take a look at http://docs.oracle.com/javase/1.5.0/docs/api/org/w3c/dom/Node.html#getAttributes()

Before you do this Node id = att.item(0); take a look at the object type of Node first by doing System.out.println(first); your probably going to see that this is a text element and not an element.

What you've done when you said Node first = doc.getFirstChild().getFirstChild(); is "give me the first child of the first element, which is probably a text element. What you should be doing is checking for ELEMENT nodes like this, only Node.ELEMENT_NODE will have non-null for getAttributes():

        NodeList nl = doc.getFirstChild().getChildNodes();
        for (int i = 0; i < nl.getLength(); i++){
            Node first = nl.item(i);
            if (first.getNodeType() == Node.ELEMENT_NODE){
                System.out.println("first:" + first);
                NamedNodeMap att = first.getAttributes();
                System.out.println("att:" + att);
            }

        }
Sign up to request clarification or add additional context in comments.

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.