【发布时间】:2022-01-22 22:16:18
【问题描述】:
对于一般的restTemplate操作,如:
ResponseEntity<ResponseVO> response = restTemplate.postForEntity(url, entity, ResponseVO.class);
if (response.getBody() != null) {
String url = response.getBody().getUrl();
重复使用 ResponseEntity 时,我收到了错误的声纳警告:
A "NullPointerException" could be thrown; "getBody()" can return null. sonarlint(java:S2259)
另外,如果我重构并引入一个变量,则不会出现警告:
ResponseVO body = response.getBody();
if (body != null) {
String url = body.getUrl();
我不需要添加response != null,如果添加会有不同的(ok)警告:
Remove this expression which always evaluates to "true" [+3 locations]sonarlint(java:S2589)
是 SonarLint 错误吗?为什么它会警告 NullPointerException?
我怀疑它与 HttpEntity @Nullable 定义有关
@Nullable
public T getBody() {
我在jira.sonarsource没有发现类似的问题
【问题讨论】:
-
好吧,至少,如果结果
ResponseVO是可变的并且某些其他线程更改了它的值,那么对getBody()的两个后续调用可能返回不同的结果。如果您先将其存储在局部变量中,这是不可能的。但是,我不知道这是否是 SonarLint 在这里发出警告的原因。 -
@MCEmperor 我不认为它与 HttpEntity 的
private final T body相关
标签: java nullpointerexception sonarqube nullable sonarlint