Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Possible null pointer dereference in [...] due to return value of called method

Tags:

java

sonarqube

Small question regarding a SonarQube flagged issue I do not understand please.

My snippet is very simple.

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

However, on the second line here, Sonarqube is telling me:

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

I am a bit unsure what this means.

Most of all, I do not know how to fix this.

Little help please?

Thank you

like image 371
PatPatPat Avatar asked Nov 16 '25 05:11

PatPatPat


1 Answers

result.getToken() might return null. So when you call result.getToken().getToken() you are calling getToken() on a null reference. Thus a NullPointerException will be thrown.

So you could do something like

YourClass token = result.getToken();
if(token != null) {
    String vaultToken = token.getToken(); // whatever you want to do with it
}
else {
    // error handling
}
like image 50
geanakuch Avatar answered Nov 17 '25 17:11

geanakuch



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!