我正在通过 Sonarqube 在我们的代码上运行 findbugs,我收到空指针取消引用的错误:
There is a branch of statement that, if executed, guarantees that a null value will be dereferenced.
错误的代码很简单:
public static boolean isBigDecimalDifferent(BigDecimal x, BigDecimal y) {
return (x != null || y != null)
&& ((x != null && y == null) || (x == null && y != null) || x.compareTo(y) != 0);
}
我想知道这怎么可能。唯一可能出现 NPE 的地方是调用 x.compareTo(y) 时,但是如果 x=null 那么 Java 将永远不会分析该分支,对吗?
这是一个错误,还是我遗漏了有关 Java 分析此语句的方式的某些信息?
更新
感谢您的输入。我最终建议他们将其更改为:
if (x!=null && y != null)
return x.compare(y);
else
return x!=y;
我觉得更清楚一点。如果没有人同意更改,我将按照建议进行操作并忽略该问题,即使我宁愿避免这种情况。
最佳答案
逻辑对于 FindBugs 来说太复杂了,这里出错了。你是对的,你已经在该代码中防御了取消引用 null
。
我会简化它,以便 FindBugs 理解它,这样任何后续的人类读者也可以轻松地弄清楚它在做什么:
public static boolean isBigDecimalDifferent(BigDecimal x, BigDecimal y) {
if (x == null) {
return y != null;
}
if (y == null) {
return x != null;
}
return x.compareTo(y) != 0;
}
旁注:通常,您会有一种方法来检查是否相等,如果您想检查是否不相等,请使用!
。
在您的评论中:
Unfortunately, it is legacy code that I haven't authority to change (I do wish I could!)
然后你必须注意到 FindBugs 无法解决这个问题,并在你的 FindBugs 设置中将其设为异常(exception)(described in this question's answers)。
关于java - 为什么 findbugs 在此代码中抛出空指针取消引用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34609655/