我有一些类似于以下代码段的代码:
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.
我的问题是:
- 在此示例中,我可以将其视为误报吗?
- 将空测试放在单独的方法中是一种好习惯吗?实际的空值检查方法比示例方法长一点,所以我不想到处重复代码。
谢谢!