DO NOT REPLY TO THIS EMAIL, BUT PLEASE POST YOUR BUG 
RELATED COMMENTS THROUGH THE WEB INTERFACE AVAILABLE AT
<http://nagoya.apache.org/bugzilla/show_bug.cgi?id=14306>.
ANY REPLY MADE TO THIS MESSAGE WILL NOT BE COLLECTED AND 
INSERTED IN THE BUG DATABASE.

http://nagoya.apache.org/bugzilla/show_bug.cgi?id=14306

NullPointerException in CompareToBuilder

           Summary: NullPointerException in CompareToBuilder
           Product: Commons
           Version: 1.0 Final
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: Normal
          Priority: Other
         Component: Lang
        AssignedTo: [EMAIL PROTECTED]
        ReportedBy: [EMAIL PROTECTED]


CompareToBuilder does not seem to handle nulls well at all.

In the methods:

public CompareToBuilder append(Object lhs, Object rhs)
public CompareToBuilder append(Object[] lhs, Object[] rhs)

If either the lhs or rhs parameters are null, the code is set up to throw a 
NullPointerException instead of evaulating on the basis of null.  This requires 
that all object be vetted before they be placed in a sorting collection, not 
using the CompareToBuilder class, modifying the source code, or subclassing 
CompareToBuilder.  We worked around this by subclassing CompareToBuilder and 
overriding the methods in question.

The following lines of code (in both methods) are the cause of the exception:

        if (comparison != 0) {
            return this;
        }
        if (lhs == rhs) {
            return this;
        }
        if (lhs == null || rhs == null) {
            throw new NullPointerException();
        }

The 'if' statement should be replaced with (?) :

        if (lhs == null && rhs != null) {
            comparison = -1;
            return this;
        }
        if (lhs != null && rhs == null) {
            comparison = 1;
            return this;
        }

--
To unsubscribe, e-mail:   <mailto:commons-dev-unsubscribe@;jakarta.apache.org>
For additional commands, e-mail: <mailto:commons-dev-help@;jakarta.apache.org>

Reply via email to