1

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?

4 Answers 4

1

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

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

Comments

0

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.

Comments

0

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

Comments

0

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

Comments

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.