Sonarlint 警告我可能出现 NullPointerException,但在我看来,我做了必要的事情来防止它?

问题描述 投票:0回答:1

我已经编写了这段可以正常工作的代码,但是Sonarlint警告我不要使用

wsr.getBody().getWorkspace()
,它可能会导致
NullPointerException
上的
wsr.getBody()
,它可能会产生空值,它说。

public WorkspaceSummary getWorkspace(String workspaceName) {
   try {
      ResponseEntity<GetWorkspaceResponse> wsr = this.workspacesApi.getWorkspaceWithHttpInfo(workspaceName, this.quiet);

      if (wsr == null || wsr.getBody() == null) {
         throw new RuntimeException("getWorkspaceWithHttpInfo a renvoyé une réponse nulle");
      }

      return wsr.getBody().getWorkspace();
   }
   catch(HttpClientErrorException e) {
      [...]
   }
}

但我相信不会。在我看来,我做了必要的事情来阻止它。

有时我们看不到明显的东西,这就是为什么我问你我是否错过了什么。

java intellij-idea nullpointerexception sonarlint
1个回答
0
投票

当前的解决方法是@experimentunit1998X 建议的,不会产生警告。

ResponseEntity<GetWorkspaceResponse> wsr = this.workspacesApi.getWorkspaceWithHttpInfo(workspaceName, this.quiet);
GetWorkspaceResponse response = (wsr != null) ? wsr.getBody() : null;

if (response == null) {
   throw new RuntimeException("getWorkspaceWithHttpInfo a renvoyé une réponse nulle");
}

return response.getWorkspace();
© www.soinside.com 2019 - 2024. All rights reserved.