• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Null Compare  RSS feed

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ei... i need help, i can't figure this one out, i have a code that will check for null values, i can't seem to catch it though... here's the code

private String checkNull (Object value){
System.out.println("checking if null...");
if ((value == null)||(value.equals("null"))){
System.out.println("replacing : " + value.toString() + " with empty");
return "empty";
} else {
System.out.println(value.toString());
return value.toString();
}
}

Also, what's the best way to test for null... Thanks in advance..
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

if ((value == null)||(value.equals("null")))



Since the 'or' condition evaluates both the expressions in your if clause, a NullPointerException would be thrown in the second half of your 'if' clause.

A better way to check would be:

[ March 13, 2006: Message edited by: Savio Fernandes ]
 
Ranch Hand
Posts: 456
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Savio Fernandes:


[ March 13, 2006: Message edited by: Savio Fernandes ]



hi Savio,

you are wrong with this. && and || operators will both stop evaluating the term as soon as the result is clear.

so

is perfectly NPE-safe. it's called short-curcuit-operators, if i remember SJCP-exam correctly...

;-)

regards,
jan
 
Ranch Hand
Posts: 1704
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Savio Fernandes:


[ March 13, 2006: Message edited by: Savio Fernandes ]



I feel Den code is perfect and gives result what he is expecting.
if ((value == null)||(value.equals("null")))

Savio your code unnecessarily adding extra else loop which is not required.
 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

This line however is not NPE safe. If value == null then you can't call value.toString() without getting a NullPointerException.
 
Kj Reddy
Ranch Hand
Posts: 1704
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Garrett, the above code NPE safe. If value == null it do not evaluate value.equals("null") as we are using short circuit opertaor "||"
[ March 13, 2006: Message edited by: KJ Reddy ]
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by KJ Reddy:


Garrett, the above code NPE safe. If value == null it do not evaluate value.equals("null") as we are using short circuit opertaor "||"

[ March 13, 2006: Message edited by: KJ Reddy ]


I wasn't talking about that line, I was talking about this line:


Consider the following:


This code fails with the following error message:

checking if null...
Exception in thread "main" java.lang.NullPointerException
at NullTest.checkNull(NullTest.java:8)
at NullTest.main(NullTest.java:25)
 
Kj Reddy
Ranch Hand
Posts: 1704
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Garrett Rowe:

I wasn't talking about that line, I was talking about this line:




You are right Garrett, sorry I misunderstood. Thanks for correcting me.
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Incedentally, the work-around for this is just removing the toString() call. If the value is not null, the println() method will call toString() on it anyway, and if it is, it will just print null.

O/P =
checking if null...
replacing : null with empty
empty
 
Den Garcia
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
THanks for the reply guys...
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!