- Language: Java
- Rule: [javabugs:S6555] Null pointers should not be dereferenced
- SonarQube environment:
- SonarQube Server: v2025.2 (105476)
- SonarQube for IDE: 10.24.0.81420 for IntelliJ
The rule triggers in this example, despite the use of Spring’s @NonNullApi
at package level.
class SonarExample {
public void triggerFalsePositive(
// 1: Parameter ourObject is received
OurClass ourObject
) {
// 2: 'otherMethod' is called
// 3: Value is passed as parameter ourObject
otherMethod(ourObject);
// 7: The access on a value that can be null
// FALSE POSITIVE TRIGGERS HERE
ourObject.doSomething();
}
private void otherMethod(
// 4: Parameter ourObject is received
OurClass ourObject
) {
// 6: Taking true branch
if (
// 5: Assuming 'ourObject' is null
ourObject == null
) {
// Just checking for null is enough to trigger the sonar violation
}
}
static class OurClass {
void doSomething() {
// Do something useful
}
}
}
Interestingly (to me, at least!) SonarQube is obviously aware that ourObject
cannot be null
, because this example also triggers [java:S2583] Conditionally executed code should be reachable:
private void otherMethod(
// 1: Implies 'ourObject' can not be null.
OurClass ourObject
) {
if (
// 2: Expression is always false.
ourObject == null
) {
I believe this example is different to the one in javabugs:S6555 gets confused by passing NonNull argument to Nullable function. But feel free to close this as a duplicate, if you believe the underlying issue is the same.