I have a code which may assign null to a BigInteger. I need to check if it is null or not.
I've tried the following things, and they do not work:
==
will just check the reference, not the value.BigInteger x = BigInteger.ONE; if(x== null) { System.out.println( x ); }
Output of above is it prints x. (Somehow the boolean condition is satisfied, even though x is not null).
Following gives NullPointerException upon comparing
BigInteger x = BigInteger.ONE; BigInteger myNull = null; if(x.compareTo(myNull) == 0 ) { System.out.println( x ); }
Another NPE:
BigInteger x = BigInteger.ONE; if(x.compareTo(null) == 0) { System.out.println( x ); }
How do I check if a BigInteger is null properly?
.compareTo(arg)
throws a NullPointerException ifarg
isnull
.You should check if
arg
isnull
prior to calling the method.