我已经编写了这段可以正常工作的代码,但是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) {
[...]
}
}
但我相信不会。在我看来,我做了必要的事情来阻止它。
有时我们看不到明显的东西,这就是为什么我问你我是否错过了什么。
当前的解决方法是@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();