Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Beancomparator with null values

Posted on 2004-10-25
23
Medium Priority
?
1,015 Views
Last Modified: 2012-06-27
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?
0
Comment
Question by:padmasri99
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 7
  • 5
23 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 12404857
Can you show code?
0
 

Author Comment

by:padmasri99
ID: 12404965
comparator = new BeanComparator(id, new ComparableComparator());
Collections.sort(arraylistofvalues, comparator);
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12405003
What's the idea of 'ComparableComparator'? Why not just

Collections.sort(arraylistofvalues);

?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 92

Expert Comment

by:objects
ID: 12405016
If fields contain null then you need to supply a Comparator or ChainedComparator in constructor.
0
 
LVL 92

Expert Comment

by:objects
ID: 12405040
ComparableCompartator will throw a CCE if any field is null.
You need to use a Comparator that supports null.
0
 

Author Comment

by:padmasri99
ID: 12405048
Which comparators support for null?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12405155
A Comparator should not throw a CCE because of a null. It may well be because something is *not* Comparable
0
 

Author Comment

by:padmasri99
ID: 12405177
I see this exception when the arrylist is containing nulls.Otherwise no exception
0
 
LVL 92

Expert Comment

by:objects
ID: 12405227
> 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
 

Author Comment

by:padmasri99
ID: 12405275
Yes
0
 

Author Comment

by:padmasri99
ID: 12405297
When I am using NullComparator I am not getting the exception. Will this create any other problems for my sorting?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12405328

"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
 
LVL 92

Expert Comment

by:objects
ID: 12405341
yes use a NullComparator:

new NullComparator(new ComparableComparator());
0
 

Author Comment

by:padmasri99
ID: 12405351
I believe CCE is thrown if objects are not comparable as well as if the values are containg nulls.
0
 

Author Comment

by:padmasri99
ID: 12405362
Whatz the difference between using new NullComparator() and new NullComparator(new ComparableComparator());
0
 
LVL 92

Expert Comment

by:objects
ID: 12405405
It passes a Comparator to use if the objects aren't null.
0
 

Author Comment

by:padmasri99
ID: 12405419
I need to do the soring using ReverseComparator also. In that case do I need to use three comparators?
0
 
LVL 92

Accepted Solution

by:
objects earned 1000 total points
ID: 12405455
try:

tnew NullComparator(new ReverseComparator(new ComparableComparator()));
0
 
LVL 92

Expert Comment

by:objects
ID: 12405468
or depending on your sort rules:

new ReverseComparator(new NullComparator(new ComparableComparator()));
0
 

Author Comment

by:padmasri99
ID: 12415482
Thanks it works
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question