• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

null pointer exception to get first child node while xml parsing

 
shalu pareek
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Paul Clapham
Sheriff
Posts: 21322
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic