【问题标题】:Possible null pointer dereference in [...] due to return value of called method由于被调用方法的返回值,可能在 [...] 中取消引用空指针
【发布时间】:2021-08-06 09:16:50
【问题描述】:

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

我的sn-p很简单。

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

然而,在第二行,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

我有点不确定这是什么意思。

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

请帮忙?

谢谢

【问题讨论】:

    标签: java sonarqube


    【解决方案1】:

    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
    }
    

    【讨论】:

      猜你喜欢
      • 2021-12-28
      • 2016-09-14
      • 2011-05-27
      • 2015-11-05
      • 1970-01-01
      • 1970-01-01
      • 2017-02-12
      • 1970-01-01
      • 2018-06-18
      相关资源
      最近更新 更多