Rule:
S2259 Null pointers should not be dereferenced
Environment:
SonarQube Version: 10.0.0.68432
Description:
While analyzing Java code using SonarQube, I found that it does not accurately report null pointer dereferences in non-static methods. The following examples illustrate the issue:
Example 1: (Issue reported by S2259)
public class NullPointerDereference {
public static void hasArguments(String name) {
int length = name.length(); // report S2259, NullPointerException
System.out.println("Name length: " + length);
}
public static void main(String[] args) {
String name = null;
hasArguments(name);
}
}
Example 2: (Issue not reported by S2259)
public class NullPointerDereference {
public void hasArguments(String name) {
int length = name.length(); // FN
System.out.println("Name length: " + length);
}
public static void main(String[] args) {
String name = null;
NullPointerDereference nullPointer = new NullPointerDereference();
nullPointer.hasArguments(name);
}
}
In the first example, SonarQube correctly identifies the null pointer dereference in the static method, whereas in the second example, it fails to report the same issue when the method is non-static. This inconsistency in handling S2259 between static and non-static methods should be addressed to ensure accurate analysis of potential null pointer dereferences.