java.util.Collections.binarySearch(List, Object, Comparator c) throws NPE when 
c is null
----------------------------------------------------------------------------------------

         Key: HARMONY-94
         URL: http://issues.apache.org/jira/browse/HARMONY-94
     Project: Harmony
        Type: Bug
  Components: Classlib  
    Reporter: Svetlana Samoilenko


According to j2se 1.4.2 and 1.5 specification for 
java.util.Collections.binarySearch(List, Object, Comparator c) method null 
value of Comparator c indicates, that the elements' natural ordering should be 
used.
Harmony throws NPE in this case that contradicts specification.

Code to reproduce: 
import java.util.*; 
public class test2 {          
public static void main(String args[]){ 
        LinkedList lst = new LinkedList(); 
        lst.add(new Integer(30)); 
        Collections.sort(lst, null); 
        int i = Collections.binarySearch(lst, new Integer(2), null); 
        System.out.println("Index of search key =" + i); 
    } 
}
Steps to Reproduce: 
1. Build Harmony (check-out on 2006-01-30) j2se subset as described in 
README.txt. 
2. Compile test2.java using BEA 1.4 javac 
> javac -d . test2.java 
3. Run java using compatible VM (J9) 
> java -showversion test2

Output: 
C:\tmp>C:\jrockit-j2sdk1.4.2_04\bin\java.exe -showversion test2 
java version "1.4.2_04" 
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2_04-b05) 
BEA WebLogic JRockit(TM) 1.4.2_04 JVM (build ari-31788-20040616-1132-win-ia32, 
Native Threads, GC strategy: parallel) 
Index of search key =-1

C:\tmp>C:\harmony\trunk\deploy\jre\bin\java -showversion test2 
(c) Copyright 1991, 2005 The Apache Software Foundation or its licensors, as 
applicable. 
java.lang.NullPointerException
        at java.util.Collections.binarySearch(Collections.java:1347)
        at test2.main(test2.java:8)

 Suggected fix:

Index: trunk/modules/luni/src/main/java/java/util/Collections.java
===================================================================
---   trunk/modules/luni/src/main/java/java/util/Collections.java       
(revision 377385)
+++ trunk/modules/luni/src/main/java/java/util/Collections.java      (working 
copy)
@@ -1340,6 +1340,9 @@
             */
            public static int binarySearch(List list, Object object,
                                    Comparator comparator) {
+                if ( comparator== null) {
+                    return Collections.binarySearch(list, object);
+                }
                        if (!(list instanceof RandomAccess)) {
                                    ListIterator it = list.listIterator();
                                    while (it.hasNext()) {                      
              

 Suggested junit test case:
------------------------ CollectionsTest.java 
------------------------------------------------- 
import junit.framework.*; 
import java.util.*; 

public class CollectionsTest extends TestCase { 
    public static void main(String[] args) { 
        junit.textui.TestRunner.run(CollectionsTest.class); 
    } 
    public void test_binarySearch () { 
        LinkedList lst = new LinkedList(); 
        lst.add(new Integer(30)); 
        Collections.sort(lst, null); 
        assertEquals(-1, Collections.binarySearch(lst, new Integer(2), null));
    } 

}



-- 
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators:
   http://issues.apache.org/jira/secure/Administrators.jspa
-
For more information on JIRA, see:
   http://www.atlassian.com/software/jira

Reply via email to