我有一些类似于以下代码段的代码:
public void foo(Order o) {
...
checkInput(o, "some error message");
doSomehing(o.getId());
}
private void checkInput(Object o, String message) {
if (o == null) {
throw new SomeRuntimeException(message);
}
}
我让Findbugs报告了'NP_NULL_ON_SOME_PATH'问题。
以下是描述:
There is a branch of statement that, if executed, guarantees that a null value will be dereferenced, which would generate a NullPointerException when the code is executed. Of course, the problem might be that the branch or statement is infeasible and that the null pointer exception can't ever be executed; deciding that is beyond the ability of FindBugs.
我的问题是:
谢谢!
答案 0 :(得分:0)
看起来FindBugs无法检测到这种情况,至少在Eclipse中是2.0.2。一种解决方法是从checkError
返回值,并使用@Nonnull
注释该方法。
public void foo(Order o) {
...
doSomehing(checkInput(o, "some error message").getId());
}
@Nonnull
private Order checkInput(Order o, String message) {
if (o == null) {
throw new SomeRuntimeException(message);
}
...
return o;
}