commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bugzi...@apache.org
Subject DO NOT REPLY [Bug 13785] New: - Calling CollectionUtils.cardinality with null obj argument leads to NPE
Date Sat, 19 Oct 2002 20:27:10 GMT
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=13785>.
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=13785

Calling CollectionUtils.cardinality with null obj argument leads to NPE

           Summary: Calling CollectionUtils.cardinality with null obj
                    argument leads to NPE
           Product: Commons
           Version: Nightly Builds
          Platform: Other
        OS/Version: Other
            Status: NEW
          Severity: Normal
          Priority: Other
         Component: Collections
        AssignedTo: commons-dev@jakarta.apache.org
        ReportedBy: sye@mail.ru


There is a bug in cardinality method, which shows, when obj parameter is null;
It leads to NullPointerException.

In order to reveal it, add line

assertEquals(0, CollectionUtils.cardinality(null, _b));

to testCardinality in TestCollectionUtils.

One variant of correct implementation is following:

public static int cardinality(Object obj, final Collection col) {
       int count = 0;
       Iterator it = col.iterator();
       if(null==obj){
           while(it.hasNext()){
               Object elt = it.next();
               if(null==elt){
                   count++;
               }
           }
       }else{
           while(it.hasNext()) {
               Object elt = it.next();
               if(obj.equals(elt)) {
                   count++;
               }
           }
       }
       return count;
   }

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


Mime
View raw message