我的 Eclipse Oxygen 告诉我在这段代码的第 7 行有一个 Potential null pointer access: The variable list may be null at this location
。
在我看来,list
或 list2
都不可能为 null。
public static <T> boolean equalsLists(List<T> list, List<T> list2, Equals<T> equals)
{
if (list == list2)
return true;
if (list == null ^ list2 == null)
return false;
if (list.size() != list2.size()) //here eclipse highlights a Potential null pointer access for both list and list2
return false;
for (int i = 0; i < list.size(); i ++)
if (equals == null && !equals(list.get(i), list2.get(i)) || equals != null && !equals.equals(list.get(i), list2.get(i)))
return false;
return true;
}
编辑:正如我在回答评论的评论中所写,很明显,使用 OR 而不是 XOR 将继续工作,并且我已经测试 Eclipse 正确地停止预测可能的空指针。 也许我的问题还不清楚。我不是在问如何让 Eclipse 停止警告我,而是 Eclipse 是否有适当的理由警告我。
最佳答案
是的,这是错误的。它不明白您的初始 list == list2
早期返回与您随后的 XOR (^
) 检查的早期返回的组合排除了它们中的任何一个的可能性null
超出该点。
(如果您的普通维护程序员后来查看该代码时也被它绊倒,我不会感到惊讶。)
关于java - 在这种情况下,Eclipse 的 Java 的空对象预测是否错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49693743/