I'm running findbugs on our code through Sonarqube, and I'm getting an error for a null pointer dereference:
There is a branch of statement that, if executed, guarantees that a null value will be dereferenced.
The faulty code is simply this:
public static boolean isBigDecimalDifferent(BigDecimal x, BigDecimal y) {
return (x != null || y != null)
&& ((x != null && y == null) || (x == null && y != null) || x.compareTo(y) != 0);
}
I'm wondering how this is possible. The only place where an NPE is possible is when calling x.compareTo(y), but if x=null then Java will never analyse that branch, right?
Is this a bug, or am I missing something about the way Java would analyse this statement?
UPDATE
Thanks for the input. I ended up suggesting them to change it to something like:
if (x!=null && y != null)
return x.compare(y);
else
return x!=y;
which I find a bit clearer. If no one agrees to the change, I'll do as suggested and just ignore the issue, even though I'd rather avoid that.