Must-share information (formatted with Markdown):
- SonarQube Enterprise Edition Version 9.9.2 (build 77730)
- No idea how it has been deployed
- I want to understand if my issue is a false positive reporting of a potential NPE
- Looked at the analysis and tried to follow the code
(Sorry this is the first time I’ve used this community and am probably doing this terribly wrong)
Hi,
I am working on a large-scale project and our Application Security team have asked me to look at some issues regarding NPEs being reported by SonarQube.
The issue is reported as:
A “NullPointerException” could be thrown; “responseMessage” is nullable here.
I can’t paste all of the code here, but the simplified form is:
private void myMethod() {
String responseMessage = obtainResponseMessage(); // This method may return null
if(responseMessage != null) {
LOG.error("The responseMessage is " + responseMessage);
}
// Do more work here - removed since not relevant
if (HelperUtil.validString(responseMessage)) {
if (responseMessage.equals("User account locked")) {
// Report that the user account is locked
}
}
}
SonarQube complains the responseMessage could be null at the last ‘if statement’. From the above it is clear that it coule be null. However the key aspect is on the line prior to the last ‘if statement’. The call to the static method “validString” in class: HelperUtil . The code is the following:
public static boolean validString(String...strings) {
if(strings == null) {
return false;
}
for(String s : strings) {
if(s == null || s.length() == 0) { return false; }
if(s.trim().length() == 0) { return false; }
}
return true;
}
So how could the last if statement ever be reached if ‘responseMessage’ is null? Am I missing something or is this a deficiency in SonarQube I trust SonarQube but I just can’t see how this can be null.
Thanks!