Uploaded image for project: 'Commons BeanUtils'
  1. Commons BeanUtils
  2. BEANUTILS-277

BeanComparator - Unable to catch or handle NestedNullExceptions

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Minor
    • Resolution: Won't Fix
    • 1.7.0
    • None
    • Bean-Collections
    • 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

      { 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.

      Attachments

        Activity

          People

            Unassigned Unassigned
            travisgreer Travis Greer
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: