File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Null Compare Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Null Compare" Watch "Null Compare" New topic
Author

Null Compare

Den Garcia
Greenhorn

Joined: Mar 13, 2006
Posts: 2
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..
Savio Fernandes
Greenhorn

Joined: Apr 14, 2005
Posts: 16
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 ]
Jan Groth
Ranch Hand

Joined: Feb 03, 2004
Posts: 456
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
Kj Reddy
Ranch Hand

Joined: Sep 20, 2003
Posts: 1704
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.
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296

This line however is not NPE safe. If value == null then you can't call value.toString() without getting a NullPointerException.


Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Kj Reddy
Ranch Hand

Joined: Sep 20, 2003
Posts: 1704


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

Joined: Jan 17, 2006
Posts: 1296
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

Joined: Sep 20, 2003
Posts: 1704
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

Joined: Jan 17, 2006
Posts: 1296
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

Joined: Mar 13, 2006
Posts: 2
THanks for the reply guys...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Null Compare
 
Similar Threads
Checking String for special chars
Custom Validator persistently returning NULL
pls explain this notation (i think it is an IF STATEMENT
Unable to set Date field in ManagedBean
NullObject