BeanComparator - Unable to catch or handle NestedNullExceptions
---------------------------------------------------------------

                 Key: BEANUTILS-277
                 URL: https://issues.apache.org/jira/browse/BEANUTILS-277
             Project: Commons BeanUtils
          Issue Type: Bug
          Components: Bean-Collections
    Affects Versions: 1.7.0
            Reporter: Travis Greer
            Priority: Minor


I believe there's a bug in org.apache.commons.beanutils.BeanComparator.

When sorting on a nested property, a 
org.apache.commons.beanutils.NestedNullException is thrown if a null property 
is encountered.

For example, say I have a list of Actors and want to sort on "spouse.name".  If 
any one of the actors does not have a spouse (spouse == null), the sort will 
fail, throwing a NestedNullException and wrapping that in a ClassCastException.

Adding a NullComparator to the BeanComparator does not fix the problem because 
the null won't be detected until PropertyUtils.getProperty attempts to get the 
property - at which point it's too late.

There's probably a better way to fix this, but replacing the compare method 
with this seems to work for me (in addition to adding a NullComparator to 
BeanComparator):

    public int compare( Object o1, Object o2 ) {
        
        if ( property == null ) {
            // compare the actual objects
            return comparator.compare( o1, o2 );
        }
        
        try {
                Object value1 = null;
                Object value2 = null;
                try {
                    value1 = PropertyUtils.getProperty( o1, property );
                } catch (NestedNullException nne) {}
                try {
                    value2 = PropertyUtils.getProperty( o2, property );
                } catch (NestedNullException nne) {}
                return comparator.compare(value1, value2);
        }
        catch ( Exception e ) {
            throw new ClassCastException( e.toString() );
        }
    }


I apologize if this is the incorrect way to go about this - I'm new at this.


-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to