Sonar : Possible null pointer dereference due to return value of called method(声纳:由于被调用方法的返回值,可能的空指针取消引用)
问题描述
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.
这篇关于声纳:由于被调用方法的返回值,可能的空指针取消引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:声纳:由于被调用方法的返回值,可能的空指针取消引用


基础教程推荐
- 从 python 访问 JVM 2022-01-01
- 多个组件的复杂布局 2022-01-01
- 如何在 Spring @Value 注解中正确指定默认值? 2022-01-01
- 在 Java 中创建日期的正确方法是什么? 2022-01-01
- 大摇大摆的枚举 2022-01-01
- 如何在 JFrame 中覆盖 windowsClosing 事件 2022-01-01
- 验证是否调用了所有 getter 方法 2022-01-01
- 不推荐使用 Api 注释的描述 2022-01-01
- Java 实例变量在两个语句中声明和初始化 2022-01-01
- Java Swing计时器未清除 2022-01-01