[java:S2259] FP when null check is obsolete due to other conditions

Qube: Community 9.9

The Java rule S2259 complains about potential NPEs, even if not possible due to other conditions.

var o = getObject(); // Can be null.
boolean isError = false;

if (o == null)
    isError = true;

if (isError)

o.doSomething(): // FP

Don’t know, if this is asking too much from the rule. But other rules are surprisingly smart as well. So, why not? :wink:


Hi @mfroehlich,

Thank you for reporting and engaging with the community!

We are aware this rule is not ideal.
We have a ticket for something similar, you can check it here [SONARJAVA-4439] - Jira.

All the best,


Hi Irina,

thanks for the answer.

I think, this is one of the rules, that are crucial for the acceptance of the whole Sonar thing within companies. If this rule reports an issue, it should not be FP. It doesn’t report something, that can be done nicer, but real and hard bugs, that might have been missed. This is great and shows the value of Sonar for theproject and company. But if this rule reports 50% FPs, it’s a bad thing.

1 Like