java 如何修复 Findbugs 问题“保证取消引用空值” NP_GUARANTEED_DEREF
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5329036/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to fix the Findbugs issue "Null value is guaranteed to be dereferenced" NP_GUARANTEED_DEREF
提问by AGrunewald
Hi I have got some code that is reported as having the NP_GUARANTEED_DEREF issue by Findbugs. Now looking at my code I don't quite understand what is wrong with it, can anyone suggest what the problem is.
嗨,我有一些代码被 Findbugs 报告为有 NP_GUARANTEED_DEREF 问题。现在看我的代码,我不太明白它有什么问题,谁能提出问题所在。
public void test() {
String var = "";
int index = 2;
if (index == -1) {
var = String.class.getName();
if (var.length() == 0) {
var = null;
}
} else {
var = Integer.class.getName();
if (var.length() == 0) {
var = null;
}
}
if (var == null) {// FINBUGS reports on this line NP_GUARANTEED_DEREF
/*
* There is a statement or branch that if executed guarantees that a value
* is null at this point, and that value that is guaranteed to be
* dereferenced (except on forward paths involving runtime exceptions).
*/
throw new NullPointerException("NULL");
}
}
Now drilling into the Error in Findbugs it highlights the two assignments to var = null;
as cause for the bug but I don't quite understand why. It is not like I am actually doing anything with the var
object I am just doing a Null check. The example is taken from real production code but stripped of anything that wasn't needed to reproduce the error. What I am wondering if this is a false positive or not. And if not what would be an appropriate fix.
现在深入研究 Findbugs 中的错误,它突出显示了var = null;
导致错误的两个分配,但我不太明白为什么。这不像我实际上正在对var
对象做任何事情,我只是在做空检查。该示例取自真实的生产代码,但去除了重现错误不需要的任何内容。我想知道这是否是误报。如果不是,什么是适当的修复。
Here is the link to the Findbugs Bug Detail: http://findbugs.sourceforge.net/bugDescriptions.html#NP_GUARANTEED_DEREF
这是 Findbugs 错误详细信息的链接:http://findbugs.sourceforge.net/bugDescriptions.html#NP_GUARANTEED_DEREF
[UPDATE] After recieving some feedback on this issue I have now logged this as a False Positive in the Findbugs Bugtracker on Sourceforge the link is https://sourceforge.net/tracker/?func=detail&aid=3277814&group_id=96405&atid=614693
[更新] 在收到关于这个问题的一些反馈后,我现在在 Sourceforge 的 Findbugs Bugtracker 中将此记录为误报链接是https://sourceforge.net/tracker/?func=detail&aid=3277814&group_id=96405&atid=614693
Conversation about the problem will continue there.
关于这个问题的对话将在那里继续。
采纳答案by Grzegorz Oledzki
I see. I can confirm the same FB behavior on my computer. Looks strange indeed. What's funny, that if you replaced throw new NullPointerException
with throw new RuntimeException
the bug marker would disappear.
我知道了。我可以在我的计算机上确认相同的 FB 行为。看起来确实很奇怪。有趣的是,如果你throw new NullPointerException
用throw new RuntimeException
错误标记代替就会消失。
Now I think I understand what they've meant. The wording of the message is not exact, but they are warning you against a NPE. I guess they consider explicitly throwing NPE a bad practice.
现在我想我明白他们的意思了。该消息的措辞并不准确,但它们警告您不要使用 NPE。我猜他们认为明确地将 NPE 视为一种不好的做法。
回答by MeBigFatGuy
It is a bug in FindBugs, post this issue on their issue tracker page. findbugs.sf.net
这是 FindBugs 中的一个错误,将此问题发布在他们的问题跟踪页面上。findbugs.sf.net
回答by Bill
OK, what FindBugs is looking for is a statement or branch that is guaranteed to lead to a null pointer exception. Originally, we only looked for dereferences of null values. We later augmented the analysis to treat
好的,FindBugs 正在寻找的是保证会导致空指针异常的语句或分支。最初,我们只查找空值的取消引用。我们后来增强了分析以治疗
if (x == null) throw new NullPointerException()
if (x == null) throw new NullPointerException()
the same as an explicit dereference of x. This was primarily to help interprocedural analysis, so that methods that had explicit null checks for their parameters would treated the same as methods that dereference their parameters without explicit null checks, and report errors when null values are passed for such parameters.
与 x 的显式取消引用相同。这主要是为了帮助过程间分析,以便对其参数进行显式空检查的方法将与在没有显式空检查的情况下取消引用其参数的方法相同,并在为此类参数传递空值时报告错误。
So some of the text in our error msgs might need to be updated, but we really haven't found many realistic cases where it causes confusion.
因此,我们的错误消息中的某些文本可能需要更新,但我们确实没有发现许多实际情况会导致混淆。
I'm not quite sure what the purpose of the above code is. At the points where are you assigning null to var, you are creating a situation that will lead to an explicit throw of a null pointer exception further down. Is that really the behavior you want?
我不太确定上面代码的目的是什么。在您将 null 分配给 var 的地方,您正在创建一种情况,该情况将导致进一步显式抛出空指针异常。这真的是你想要的行为吗?
回答by jzd
Looking closer into the definition of the error message here, it says:
仔细查看此处错误消息的定义,它说:
There is a statement or branch that if executed guarantees that a value is null at this point, and that value that is guaranteed to be dereferenced (except on forward paths involving runtime exceptions)
有一个语句或分支,如果执行,则保证此时的值为空,并且保证该值被取消引用(涉及运行时异常的前向路径除外)
Which makes me think it is either just letting you know var is going to be null or something actually is making findbugs think that var is referenced inside the if statement.
这让我觉得它要么只是让你知道 var 将是空的,要么实际上让 findbugs 认为 var 是在 if 语句中引用的。
The code you posted looks fine, I would double check that var is not accessed in the true code.
您发布的代码看起来不错,我会仔细检查在真正的代码中没有访问 var。
The only thing I might change is to write the comparision backwards like so:
我可能会改变的唯一一件事是像这样向后写比较:
if (null == var)
That way it is obvious if you leave out one of the =
's/
这样,很明显,如果您省略了=
's/