File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes using jakarta nullcomparator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "using jakarta nullcomparator" Watch "using jakarta nullcomparator" New topic
Author

using jakarta nullcomparator

Mike Ottinger
Ranch Hand

Joined: Jan 11, 2002
Posts: 125
Hi Folks,

Has anybody used Jakarta's NullComparator for sorting objects with null fields? I'm getting nullpointerexceptions when trying it out. I have a simple List of bean objects where one of the fields in one of the bean objects is null, I then try code like this:



Am I using this wrong?


SCJP 1.4 SCJD 1.5
Ryan McGuire
Ranch Hand

Joined: Feb 18, 2005
Posts: 1012
    
    3
I don't think the problem is with the NullComparator. I'd bet a dollar that one of the Beans in testCollection is returning null for getFirstName(). Can you iterate through that List and print those out?



Do you get a stack trace with the NPE? It dies when the NullComparator.compare calls BeanComparator.compare right?
[ August 01, 2005: Message edited by: Ryan McGuire ]
Mike Ottinger
Ranch Hand

Joined: Jan 11, 2002
Posts: 125
Hi Ryan,


You're correct, one of the first name values *is* null in the collection. Then I'm misunderstanding the use for a NullComparator, I thought it was supposed to handle this; in other words, if a bean in the collection has a null value for the sorting field, then the bean is placed either at the front or the back of the collection. Does the "null" in NullComparator refer to the entire bean being null in the collection? If I want a comparator to work under the behavior I described above, then I need to provide my own implementation?
Ryan McGuire
Ranch Hand

Joined: Feb 18, 2005
Posts: 1012
    
    3
The NullComparator only takes the Object references in the List to be sorted into account. It doesn't look into the Objects for nulls. Instead, if it wants to compare two non-null List elements, it calls (in this case) the BeanComparator compare method for a greater/equal/less decision.

Given the constructor you used for the BeanComparator, it's then going to call getFirstName() on the two argument Objects and call something like firstName1.compareTo(firstName2). Since getFirstName() returned a null for one of the Beans, that compareTo call throws a NPE.

How to fix it? Hmmm... that's a tricky one. I don't think a solution can be built from existing building blocks. You may have to write your own comparator that A) compares values for getFirstName() and B) does the right thing with nulls. Maybe something like...



Or you might want to throw the ClassCastException if one of the arguments isn't a MyBeanClass.
[ August 02, 2005: Message edited by: Ryan McGuire ]
Mike Ottinger
Ranch Hand

Joined: Jan 11, 2002
Posts: 125
Hi Ryan,


Thanks for your help. I had actually written my own comparator implementation a while ago and had stumbled on Jakarta's work when I was looking for a way to provide multi-column sorting. My implmentation handles null comparisons nicely, so future work that involves sorting will use my comparator with jakarta's comparatorchain object. It works out fine. Once again, thank you for the help!

- M
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: using jakarta nullcomparator