由于被调用方法的返回值,[...] 中可能存在空指针取消引用

Pat*_*Pat 4 java sonarqube

关于 SonarQube 标记问题的小问题我不明白。

我的片段非常简单。

VaultTokenResponse  result = getWebClient().mutate().baseUrl(vaultUrl).build().post().retrieve().bodyToMono(VaultTokenResponse.class).block();
 
String              vaultToken     = result.getToken().getToken();
Run Code Online (Sandbox Code Playgroud)

然而,在第二行,Sonarqube 告诉我:

findbugs:NP_NULL_ON_SOME_PATH_FROM_RETURN_VALUE Style - Possible null pointer dereference due to return value of called method

The return value from a method is dereferenced without a null check, and the return value of that method is one that should generally be checked for null. This may lead to a NullPointerException when the code is executed
Run Code Online (Sandbox Code Playgroud)

我有点不确定这意味着什么。

最重要的是,我不知道如何解决这个问题。

请帮忙一点?

谢谢

gea*_*uch 5

result.getToken()可能会返回 null。因此,当您调用时,result.getToken().getToken()您正在调用getToken()空引用。因此将抛出 NullPointerException。

所以你可以做类似的事情

YourClass token = result.getToken();
if(token != null) {
    String vaultToken = token.getToken(); // whatever you want to do with it
}
else {
    // error handling
}
Run Code Online (Sandbox Code Playgroud)