user2330570
user2330570

Reputation: 39

Trouble reading an XML file in java?

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.

Upvotes: 0

Views: 99

Answers (2)

Romski
Romski

Reputation: 1942

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>

Upvotes: 1

rolfl
rolfl

Reputation: 17707

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

Upvotes: 1

Related Questions