【问题标题】:Help on a NullPointerException error关于 NullPointerException 错误的帮助
【发布时间】:2010-10-21 09:29:19
【问题描述】:

我一直在尝试从here修改JSP中的以下代码:

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

在“if (n.getNodeValue() != null){”行我收到“NullPointerException”错误。我不明白为什么我在尝试测试 Null 并跳过它们时收到该错误。

谁能帮我解决这个问题?

【问题讨论】:

    标签: xml jsp


    【解决方案1】:

    从您的 while 循环退出时,n 可能为空。因此 n.getNodeValue() 可能会给你的 NPE。

    【讨论】:

      【解决方案2】:

      当 n == null 时,您的 while 循环也会退出。因此,在这种情况下,您的“n”可能为空。在最后一个 IF 条件中检查 n != null。

      【讨论】:

        【解决方案3】:

        可能是因为 n.getNodeName() 为 null 或循环后 n 为 null。

        【讨论】:

          【解决方案4】:

          node.getParentNode() 可以返回 null 吗?如果可以,您的 n 可能为空。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2021-09-24
            • 2011-05-31
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多