2010-10-21 95 views
1

我一直在試圖從here修改JSP下面的代碼:幫助的NullPointerException錯誤

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()); 
} 

在 「如果(!n.getNodeValue()= NULL){」 行,我收到了「 NullPointerException「錯誤。我不明白爲什麼我得到這個錯誤,因爲我試圖測試Null並跳過它們。

任何人都可以幫我解決這個問題嗎?

回答

0

你的while循環也會在n == null時退出。因此,在這種情況下,你的'n'是無效的。在最後的IF條件中檢查n!= null。

0

很可能是因爲n.getNodeName()爲空或循環後n爲空。

1

退出while while循環時,n可能爲空。因此n.getNodeValue()可能會給你的NPE。

0

可以node.getParentNode()返回null嗎?如果它可以你的n可能爲空。