wood burning stoves*
The moose likes Java in General and the fly likes comparing two values with equalsIgnoreCase and equals with NULL as first value. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "comparing two values with equalsIgnoreCase and equals with NULL as first value." Watch "comparing two values with equalsIgnoreCase and equals with NULL as first value." New topic
Author

comparing two values with equalsIgnoreCase and equals with NULL as first value.

prince davies
Ranch Hand

Joined: May 08, 2009
Posts: 74


The above code returns me NULL POINTER EXCEPTION ,since the first element in the equalsIgnoreCase and equals is a null;
Why does it throw Null Pointe Exception?


java.lang.NullPointerException
at EqualIgnoreForNull.main(EqualIgnoreForNull.java:11)
Exception in thread "main"





HERE the second element in the equalsIgnoreCase and equals is null and the first element has value,but it does not throw NullPointerException
Why it does behave like this?
it returns me values.
different
different1


How do I compare two values?
If one is null and other has values,if these are assigned at run time ; how do I compare with equalsIgnoreCase and equals() ?

Apparently null is different from any other value,so I HAVE TO EXPECT null and other values in both elements. This happends for only first element.

How do I write a refined code for comparing two values considering or expecting null ?

if I use the following way,its not correct


Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36475
    
  16
Find the java.util.Objects class, which has equals() methods overloaded to take two parameters, and can cope with null values. Beware: it is only available in Java7. Or you can use an if (x != null) ... test.
rk sharma
Ranch Hand

Joined: Jun 25, 2011
Posts: 50

Hello Davies,
You should not call equals() method using the reference which points to "null".
In the first case yo will get NullPointerException obviously. but the second approach is correct. see the java SE API.


Regards,

RK
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: comparing two values with equalsIgnoreCase and equals with NULL as first value.
 
Similar Threads
How to make the reference object also null when the method makes its parameter null value?
comparing two objects by overriding equals and hashCode
Regarding the Array Declaration
jsch, trying to open tunnel and connect sftp
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Array index out of range: 0