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.
这篇关于声纳:由于被调用方法的返回值,可能的空指针取消引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:声纳:由于被调用方法的返回值,可能的空指针取消引用
基础教程推荐
- 在java中使用xpath和selenium解析HTML表格数据 2022-01-01
- Maven:无效的目标版本:10 2022-01-01
- 将 double 转换为 Int,向下舍入 2022-01-01
- 控制台应用程序中的 Java 键盘输入解析 2022-01-01
- 在springboot中如何给mybatis加拦截器 2023-04-29
- JPA惰性列表上的流 2022-01-01
- Java ECDSAwithSHA256 签名长度不一致 2022-01-01
- doFilter()是在servlet的工作完成之前还是之后执行的? 2022-01-01
- 如何在相机中应用自定义滤镜 [Surfaceview 预览]. 2022-01-01
- 将 Windows 证书导入 Java 2022-01-01
