要摆脱NP_NULL_ON_SOME_PATH_FROM_RETURN_VALUE? [英] Getting rid of NP_NULL_ON_SOME_PATH_FROM_RETURN_VALUE?
问题描述
我在此代码段中得到了NP_NULL_ON_SOME_PATH_FROM_RETURN_VALUE
I'm getting NP_NULL_ON_SOME_PATH_FROM_RETURN_VALUE
in this snippet
final Integer id = Ints.tryParse(idString);
FailReason.NO_SUCH_THING.checkCondition(id!=null);
something.setId(id.intValue());
Here, checkCondition
works just like Preconditions.checkArgument except for it throws my own exception. Throwing NPE or IAE is not appropriate here, as it's checking an external input rather than a programming error.
FindBugs抱怨"方法的返回值被取消引用而没有空检查,并且该方法的返回值通常应检查为空.这可能会导致NullPointerException.被执行."
FindBugs complains that "The return value from a method is dereferenced without a null check, and the return value of that method is one that should generally be checked for null. This may lead to a NullPointerException when the code is executed."
我可以教FindBugs说,当参数为false
时,checkCondition
永远不会正常返回吗?
Can I teach FindBugs that checkCondition
never returns normally when the argument is false
?
推荐答案
我(通过某种方式)通过添加解决了
I (sort of) solved it by adding
assert id!=null;
这是多余的和丑陋的,但是比@SuppressFBWarnings
丑陋得多,而且更本地化.
It's redundant and ugly, but still less ugly than @SuppressFBWarnings
and more local.
这篇关于要摆脱NP_NULL_ON_SOME_PATH_FROM_RETURN_VALUE?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!