tags:

views:

17

answers:

4

I have been trying to modify the following code in JSP from here:

ArrayList arrayList=new ArrayList(); 
String  = "tagToFind";
Node n = node.getParentNode();
String printOut = "";
while (n != null && !n.getNodeName().equals(tagToFind)) {   
    n = n.getParentNode();
}
if (n.getNodeValue() != null){
    arrayList.add(n.getNodeValue());
}

on the "if (n.getNodeValue() != null){" line I get a "NullPointerException" error. I don't understand why I'm getting that error as I'm trying to test for Nulls and skip them.

Can anyone help me over come this issue?

A: 

Probably because n.getNodeName() is null or n is null after the loop.

kukudas
+1  A: 

on exit from your while loop n may be null. Hence n.getNodeValue() may give your NPE.

djna
A: 

Your while loop will exit when n == null too. Hence there are possibilities that your 'n' is null in this case. Check for n != null in your last IF condition.

Sachin Shanbhag
A: 

Can node.getParentNode() return null? If it can your n might be null.

kasten