0

Im using the following code for xml node extraction , But im getting the nullpointer exception

<root>
<place >
    <city> aaaaaa </city> 
    <state / >
    <country> zzzzzzzz </country>
<place>

</root>

I have tried the following code

NodeList nodeList = (NodeList)xPath1.compile(temp).evaluate(xmlDocument,XPathConstants.NODESET);
TempFlat = nodeList.item(0).getFirstChild().getNodeValue();

and i have googled and got this

 NodeList nodeList = (NodeList)xPath1.compile(temp).evaluate(xmlDocument,XPathConstants.NODESET);
Node nValue = (Node) nodeList.getFirstChild();

where temp is my xpath . Which is readable from a file. example

/root/place/city
   /root/place/city
   /root/place/city

but for this im getting the following error

E:\Work\java\test.java:80: cannot find symbol
symbol  : method getFirstChild()
location: interface org.w3c.dom.NodeList
  Node nValue = (Node) nodeList.getFirstChild();
                                                                     ^
1 error

I found that , this is because <state / > is empty . How can i solve this . Can anyone help me

NOTE : I have found the reason for the 2nd error i have posted , But i need to solve the Null Pointer exception

2
  • @AKS Okay , Temp is my XPATH here , for example '/root/state' Commented Mar 26, 2014 at 18:08
  • What is your XML document's root element, and how is <city> related to it? Edit your post to show the document structure all the way back to the root. Commented Mar 26, 2014 at 18:16

2 Answers 2

1

NodeList doesn't have any method getFirstChild().

http://docs.oracle.com/javase/7/docs/api/org/w3c/dom/NodeList.html

Once you have the NodeList

NodeList nodeList = (NodeList)xPath1.compile(temp).evaluate(xmlDocument,XPathConstants.NODESET);

before getting any Node from the nodeList, check for it's length

if (nodeList.getLength() > 0)
    Node node = nodeList.items(0);

Similarly, once you have the Node and you want to get the first child here, you need to be sure that the node have some childs

if (node.hasChildNodes())
    String value = node.getFirstChild().getNodeValue();

This way you can avoid null values.

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

1 Comment

Yes, I ahve seen this . I( just need a solution to solve this problem , My null pointer exception error
0

Your compile error at the bottom of your post is due to the fact that there is no getFirstChild() method for org.w3c.dom.NodeList. See the JavaDoc.

But for your question, you will need to check if the node exists before you call the getNodeValue() method. When you call getFirstChild() on the state node you will always get null because it has no children. So calling the getNodeValue() method on this null value will result in the NullPointerException you are seeing. To get around this you will check if the node exists first before you start looking for the value. Try something like the code below to fix your issue. I'm not sure what TempFlat is so I will assume it is a String variable.

String tempFlat = "DEFAULT VALUE";
if(noteList.item(0).hasChildNodes())
    tempFlat = nodeList.item(0).getFirstChild().getNodeValue();

1 Comment

Thanks for the reply ,But could you mind anz how to solve the null pointer exception , Have a look on my updated post

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.