xml – Help on a NullPointerException error – Education Career Blog

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){

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?


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


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.


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


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

Leave a Comment