File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes null comparison Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "null comparison" Watch "null comparison" New topic

null comparison

Tyler Jordan
Ranch Hand

Joined: Nov 17, 2003
Posts: 70
I have a value in an array that has certain values that are null when I do a println command on them. I need to do an statement that compares these value to null. (either that it equals or doesn't equal null). I have tried the equals method and the operators, but neither compares the values correctly. Is this a special case in Java? How should I compare these?
Chris Harris
Ranch Hand

Joined: Sep 21, 2003
Posts: 231
Hi Tyler,
Compare null values are null do the following instead of using the equals method:
if (ipArray[s] == null) //is null
if(ipArray[s] != null) // if not null.

You can't do:
as there is no instance created for the value so there will not be a pointer to the object. If you do this you will get a NullPointerException if ipArray[s] is null.
Hope that helps. Let us know if you have any more problems.

I agree. Here's the link:
subject: null comparison
It's not a secret anymore!