Consider this piece of Java code:
--------------------------------------------------------
import java.math.BigInteger;
public class Test {
void x() {
BigInteger i = BigInteger.ZERO;
if (i == null) {
i = BigInteger.ZERO;
} else {
i = BigInteger.ZERO;
}
System.out.println(new BigInteger("1").add(i));
^^^^ FindVugs detects a bug here
}
}
--------------------------------------------------------
The detected bug reads
> Bug: Null passed for nonnull parameter of java.math.BigInteger.add(BigInteger) in Test.x()
The actual problem, however, is the useless null check, as BigInteger.ZERO can never be null.
OK, to fix this I had to tell findBugs that BigDecimal.ZERO is always nonnull.
Without knowing that, FindBugs was actually being fairly clever to report the warning correctly.
Thanks for fixing this. Was it only related to BigInteger.ZERO? Because I feel that possibly, all static final values could be affected...
If the code had used field Foo.BAR, then you would want a bug reported here. It is only a false positive because BigInteger.ZERO is known to be non null.