Solved

Beancomparator with null values

Posted on 2004-10-25
912 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
Question by:padmasri99
    21 Comments
     
    LVL 86

    Expert Comment

    by:CEHJ
    Can you show code?
    0
     

    Author Comment

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

    Expert Comment

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

    Collections.sort(arraylistofvalues);

    ?
    0
     
    LVL 92

    Expert Comment

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

    Expert Comment

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

    Author Comment

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

    Expert Comment

    by:CEHJ
    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
    I see this exception when the arrylist is containing nulls.Otherwise no exception
    0
     
    LVL 92

    Expert Comment

    by:objects
    > 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
     
    LVL 86

    Expert Comment

    by:CEHJ
    0
     

    Author Comment

    by:padmasri99
    Yes
    0
     

    Author Comment

    by:padmasri99
    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

    "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
    yes use a NullComparator:

    new NullComparator(new ComparableComparator());
    0
     

    Author Comment

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

    Author Comment

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

    Expert Comment

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

    Author Comment

    by:padmasri99
    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:
    try:

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

    Expert Comment

    by:objects
    or depending on your sort rules:

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

    Author Comment

    by:padmasri99
    Thanks it works
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Suggested Solutions

    By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
    Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
    Video by: Michael
    Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
    This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

    934 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

    Need Help in Real-Time?

    Connect with top rated Experts

    24 Experts available now in Live!

    Get 1:1 Help Now