Beancomparator with null values

I am  using BeanComparator to sort on a date bean property, but I'm having an issue where sometimes that property is null. It then gives me a ClassCastException:

java.lang.ClassCastException: java.lang.ClassCastException: There were nulls in the arguments for this method.

Can someone let me know about resolving this?
padmasri99Asked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
objectsConnect With a Mentor Commented:
try:

tnew NullComparator(new ReverseComparator(new ComparableComparator()));
0
 
CEHJCommented:
Can you show code?
0
 
padmasri99Author Commented:
comparator = new BeanComparator(id, new ComparableComparator());
Collections.sort(arraylistofvalues, comparator);
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
CEHJCommented:
What's the idea of 'ComparableComparator'? Why not just

Collections.sort(arraylistofvalues);

?
0
 
objectsCommented:
If fields contain null then you need to supply a Comparator or ChainedComparator in constructor.
0
 
objectsCommented:
ComparableCompartator will throw a CCE if any field is null.
You need to use a Comparator that supports null.
0
 
padmasri99Author Commented:
Which comparators support for null?
0
 
CEHJCommented:
A Comparator should not throw a CCE because of a null. It may well be because something is *not* Comparable
0
 
padmasri99Author Commented:
I see this exception when the arrylist is containing nulls.Otherwise no exception
0
 
objectsCommented:
> A Comparator should not throw a CCE because of a null.

No reason why it shouldn't, and in this case it does as I stated above.
0
 
padmasri99Author Commented:
Yes
0
 
padmasri99Author Commented:
When I am using NullComparator I am not getting the exception. Will this create any other problems for my sorting?
0
 
CEHJCommented:

"Throws:
...
java.lang.ClassCastException - when obj1 is not a Comparable, or when ((Comparable)obj1).compareTo(obj2) does"

If that doesn't help, simply supply your own Comparator for the sort, being careful with nulls
0
 
objectsCommented:
yes use a NullComparator:

new NullComparator(new ComparableComparator());
0
 
padmasri99Author Commented:
I believe CCE is thrown if objects are not comparable as well as if the values are containg nulls.
0
 
padmasri99Author Commented:
Whatz the difference between using new NullComparator() and new NullComparator(new ComparableComparator());
0
 
objectsCommented:
It passes a Comparator to use if the objects aren't null.
0
 
padmasri99Author Commented:
I need to do the soring using ReverseComparator also. In that case do I need to use three comparators?
0
 
objectsCommented:
or depending on your sort rules:

new ReverseComparator(new NullComparator(new ComparableComparator()));
0
 
padmasri99Author Commented:
Thanks it works
0
All Courses

From novice to tech pro — start learning today.