You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by "Niall Pemberton (JIRA)" <ji...@apache.org> on 2007/05/21 00:27:16 UTC

[jira] Resolved: (BEANUTILS-277) BeanComparator - Unable to catch or handle NestedNullExceptions

     [ https://issues.apache.org/jira/browse/BEANUTILS-277?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Niall Pemberton resolved BEANUTILS-277.
---------------------------------------

    Resolution: Won't Fix

Your proposal works for your use case, but may not for others. Seems to me that having your own custom implementation of BeanComparator with the functionality you require in the compare method is the best solution in this case.

> 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: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org