Details
-
Bug
-
Status: Closed
-
Minor
-
Resolution: Won't Fix
-
1.7.0
-
None
-
None
Description
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
catch (NestedNullException nne) {}
try
catch (NestedNullException nne) {}
return comparator.compare(value1, value2);
}
catch ( Exception e )
}
I apologize if this is the incorrect way to go about this - I'm new at this.