问题描述
if (response != null && response.getBody() != null && response.getStatusCode() == HttpStatus.OK) {
return new BigDecimal(response.getBody());
}
由于上述代码中调用方法的返回值,我可能会取消引用空指针.
I am getting possible null pointer dereference due to return value of called method on above code.
有人可以告诉我确切的问题以及为什么会出现问题吗?
Can someone please let me know the exact issue and why it's an issue?
response.getBody() // returns a string value
提前致谢!如果需要任何其他详细信息,请告诉我.
Thanks in advance! Please let me know if any other details are needed.
推荐答案
Sonar 不知道连续两次调用 getBody() 会返回相同的值.
Sonar does not know that the two consecutive calls to getBody() will return the same value.
因此,从静态分析器的角度来看,第二次调用确实有可能返回 null.
So, it is really possible, from the point of view of a static analyzer, that the second call returns null.
我建议将 body 分配给一个局部变量,并且只调用一次 getter.这里是来自 Sonar 社区的参考,有人将此行为报告为错误并收到了类似的响应.
I'd recommend assigning the body to a local variable, and calling the getter only once. Here is a reference from Sonar community, where someone reported this behavior as bug and received a similar response.
静态分析器实际上无法证明这两个调用将返回相同的值,除非 response 是最终且不可变的类型.并且我尝试过的任何静态分析器都没有尝试证明这一点.
A static analyzer actually cannot prove that the two calls will return the same value, unless response is of a final and immutable type. And no static analyzer I've tried yet goes to the length of trying to prove that.
这篇关于声纳:由于被调用方法的返回值,可能的空指针取消引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!


大气响应式网络建站服务公司织梦模板
高端大气html5设计公司网站源码
织梦dede网页模板下载素材销售下载站平台(带会员中心带筛选)
财税代理公司注册代理记账网站织梦模板(带手机端)
成人高考自考在职研究生教育机构网站源码(带手机端)
高端HTML5响应式企业集团通用类网站织梦模板(自适应手机端)