• Post Reply Bookmark Topic Watch Topic
  • New Topic

Handling null values in Comparator  RSS feed

 
Shivom Shukla
Ranch Hand
Posts: 77
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I read the contract of comparable that if we use compareTo() with a null, we instantly get a NullPointerException.
But as far as Comparator is concerned, there is no null handling (I know, some say its not comparator's job or it is an interface so implementation has to be yours but still...).

I came across a solution which says if we have two objects say s1 and s2.
Now if both are null we return 0.
if s1 is null or s2 is null we return -1
and if both s1 and s2 are not null we return 1

Is this the only way?
What happens if we do not provide null handling?
Will there be a NullPointerException?

Thanks,
Shivom
 
E Armitage
Rancher
Posts: 989
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shivom Shukla wrote:
Is this the only way?
What happens if we do not provide null handling?
Will there be a NullPointerException?

Thanks,
Shivom

Comparators are there for you the developer to decide which object is greater than the other so it's up to you based on your business rules to decide what should happen when the values you are comparing have null properties. Whether there is a null pointer exception or not depends on how you wrote the comparator. N.P.E happen when you dereference a null variable. If you dereference a null in your comparator then a null pointer exception will result. If you don't then there won't be a N.P.E
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You may find the best form of null handling is to make sure the d*mn things don't get into your code in the first place.
There are some places where nulls are valid, or where they are inevitable, but is your application one of them?
 
Shivom Shukla
Ranch Hand
Posts: 77
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi E Armitage,

Thanks for your reply.
I understand this is a developer's job but i too said earlier ;)
I know, some say its not comparator's job or it is an interface so implementation has to be yours but still...).



HI Campbell,
how are you?
but is your application one of them?


No not really. I just came across this question and wanted to clarify my doubts.
It is my hobby to find out everything in java. If it takes time..so be it ;)

Thanks,
Shivom
 
K. Tsang
Bartender
Posts: 3648
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shivom Shukla wrote:I came across a solution which says if we have two objects say s1 and s2.
Now if both are null we return 0.
if s1 is null or s2 is null we return -1
and if both s1 and s2 are not null we return 1


Interesting. Here is how I see it. First ask yourself given a list of strings which has 1 null value in it. Then you do Collections.sort(theList). What happen? By the way I haven't tried it.

Now back to the question. If you do have what you said (eg both null is 0, both not null is 1 etc). then to me you are making a "NullComparator" rather than checking for null.

If both objects are null surely they are equal! Both not null then you surely need to compare the attributes of those objects. If either is null, -1 or 1 doesn't matter because practically they can't be compared.
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shivom Shukla wrote: . . . It is my hobby to find out everything in java.. . .
Tired, but otherwise very well, thank you.

And that hobby looks a good way to learn
 
Shivom Shukla
Ranch Hand
Posts: 77
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi K. Tsang,

Yeah after reading your post, I feel i am coming up with a NullComaparator

Hi Campbell,

Ranch is just the place which gives FUEL TO FIRE
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!