0

So I am having a lot of trouble reading this XML file:

<?xml version = "1.0" encoding = "UTF-8"?>
<!--this version of Eclipse dosn't support direct creation of XML files-->
<!-- you have to create one in notepad and then copy/paste it into Eclipse-->

<root testAttribute = "testValue">
    <data>Phoebe</data>
    <data>is</data>
    <data>a</data>
    <data>puppy!</data>

    <secondElement testAttribute = "testValueAgain">
        <data2>Poop</data2>
        <data2>Doopy</data2>
    </secondElement>
</root>

In my java file I get a NullPointerException in this one line. Here's the code: (I'll point out where the exception occurs)

import javax.xml.parsers.*;
import org.w3c.dom.*; 
import org.xml.sax.*;

import java.io.*;

public class Reading {
    public static void main(String args[]){
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        try {
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document doc = builder.parse(new File("res/Test.xml"));

            ////////////////////GET ELEMENTS//////////////////

            Element rootElement = doc.getDocumentElement(); 
            System.out.println("Root element: " + doc.getDocumentElement().getNodeName());
            System.out.println("testAttribute for root element: "
                + rootElement.getAttribute("testAttribute"));

            Element secondElement = doc.getElementById("secondElement");
            System.out.println("testAttribute for second element: " + 
                secondElement.getAttribute("testAttribute")); //THIS IS THE LINE

            NodeList list = rootElement.getElementsByTagName("data");

            NodeList list2 = rootElement.getElementsByTagName("data2");

            //////////////////////////////////

            for(int i = 0; i < list.getLength(); i++){
                Node dataNode = list.item(i);
                System.out.println("list index: " + i + " data at that index: " +
                dataNode.getTextContent());
            }

            for(int i = 0; i < list2.getLength(); i++){
                Node dataNode = list2.item(i);
                System.out.println("list2 index: " + i + " data at that index: " +
                dataNode.getTextContent());
            }
        }catch(ParserConfigurationException e){
            e.printStackTrace();
        }catch(IOException e){
            e.printStackTrace();
        }catch(SAXException e){
            e.printStackTrace();
        }
    }
}

Can you guys check out my code and tell me what I can do to avoid the NullPointerException? I'm really frustrated right now. Thanks!

P.S. some of you guys answered about the line above the line that got the exception. The exception occurs when I try to PRINT OUT the testAttribute value in the secondaryElement element.

1
  • <!--this version of Eclipse dosn't support direct creation of XML files--> I find that very hard to believe. What version is it? Commented Apr 29, 2013 at 2:55

2 Answers 2

1

getElementByID is not what you think it is, thus returns null (there are no id="..." attributes).

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

Comments

1

The quick answer is that your secondElement is null. The reason is because you have no element with id="secondElement". Your code expects the document to contain something like

<myelement id="secondElement">...</myelement>

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.