java - How to check if a BigInteger is null -
i have code may assign null biginteger. need check if null or not.
i've tried following things, , not work:
==
check reference, not value.biginteger x = biginteger.one; if(x== null) { system.out.println( x ); }
output of above prints x. (somehow boolean condition satisfied, though x 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 check if biginteger null properly?
there difference between null
reference , object value 0. check null
references, use:
biginteger value = getvalue(); if (value != null) { // }
to check value 0, use:
biginteger value = getvalue(); if (!biginteger.zero.equals(value)) { // }
to ensure object neither null
reference nor has value 0, combine both:
biginteger value = getvalue(); if (value != null && !value.equals(biginteger.zero)) { // }
2015-06-26: edited according @arpit's comment.
Comments
Post a Comment