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


- Eclipse 插件更新错误日志在哪里? 2022-01-01
- Safepoint+stats 日志,输出 JDK12 中没有 vmop 操作 2022-01-01
- 从 finally 块返回时 Java 的奇怪行为 2022-01-01
- Spring Boot连接到使用仲裁器运行的MongoDB副本集 2022-01-01
- 如何使用WebFilter实现授权头检查 2022-01-01
- Jersey REST 客户端:发布多部分数据 2022-01-01
- value & 是什么意思?0xff 在 Java 中做什么? 2022-01-01
- C++ 和 Java 进程之间的共享内存 2022-01-01
- 将log4j 1.2配置转换为log4j 2配置 2022-01-01
- Java包名称中单词分隔符的约定是什么? 2022-01-01