开发者

Help on a NullPointerException error

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.getParent开发者_运维知识库Node();
}
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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜