It's not a secret anymore!
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

Win a copy of Make it so: Java DB Connections & Transactions this week in the JDBC forum!
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.

subject: null comparison
It's not a secret anymore!