This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes XML and Related Technologies and the fly likes null pointer exception to get first child node while xml parsing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "null pointer exception to get first child node while xml parsing" Watch "null pointer exception to get first child node while xml parsing" New topic
Author

null pointer exception to get first child node while xml parsing

shalu pareek
Greenhorn

Joined: Nov 14, 2008
Posts: 14
Hi ,
i am trying to retrive the first child node and want to put in Map. but its throwing me null pointer exception. How to get only the first child of the xml file.
Below is my code:

if (reqNode != null) {
Node nodeList1 = reqNode.getFirstChild();

if (nodeList1 != null) leftFrstMap.put(nodeList1.getAttributes().getNamedItem("label").getNodeValue(), nodeList1.getAttributes().getNamedItem("href").getNodeValue());
}
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39576
    
  27
The first step is to ascertain which of the many object used in this code is null. Then you can start investigating why it might be null.


Ping & DNS - updated with new look and Ping home screen widget
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18155
    
    8

Often beginners with DOM assume that the getFirstChild method will return the first child which is an Element, not noticing that there is a Text node which is all whitespace before that Element in their document. This Text node is the first child.

Have a look at your document to see if that is the case.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: null pointer exception to get first child node while xml parsing
 
Similar Threads
XML Parsing
Node Value Question
XML Parsing issue.
xml updations and deletions
XML Node Parsing Problem , when Node owns NodeList using Xercer , HELP !!!!