Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7029613
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:33:43+00:00 2026-05-28T00:33:43+00:00

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

  • 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;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T00:33:44+00:00Added an answer on May 28, 2026 at 12:33 am

    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);
                }
    
            }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have class named Group i tried to test configuration: var cfg = new
I have a Class named Constants that contains all the constant variable in my
I have a class named toto which I send to a function that does
I have a class named Texture that has a pointer to a SDL_Surface. When
I have a class named Page with a private property currently called _pageData which
I have a class named baseClass. From this class I inherit a class names
I have a class named Person and in this class is the property PersonName
I have a class named project with the following data members. class Project {
I have a class named SuperclassA , and an class named ClassA . ClassA
Say I have a class named Frog, it looks like: public class Frog {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.