要摆脱NP_NULL_ON_SOME_PATH_FROM_RETURN_VALUE? [英] Getting rid of NP_NULL_ON_SOME_PATH_FROM_RETURN_VALUE?

查看:751
本文介绍了要摆脱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());

在这里,checkCondition的工作方式与

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屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆