You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by bu...@apache.org on 2002/11/06 19:21:23 UTC

DO NOT REPLY [Bug 14306] New: - NullPointerException in CompareToBuilder

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: commons-dev@jakarta.apache.org
        ReportedBy: schloss@visi.com


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