问题描述
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.
这篇关于声纳:由于被调用方法的返回值,可能的空指针取消引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!