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:

  1. == 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).

  1. following gives nullpointerexception upon comparing

    biginteger x = biginteger.one; biginteger mynull = null;  if(x.compareto(mynull) == 0 ) {     system.out.println( x ); } 
  2. 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

Popular posts from this blog

python - No exponential form of the z-axis in matplotlib-3D-plots -

php - Best Light server (Linux + Web server + Database) for Raspberry Pi -

c# - "Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types" error when deserializing class -