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?
posted 11 years ago
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: ipArray[s].equals(null) 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. Chris