Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4026541

java.awt.Container.remove(int i) throws different exceptions if i is invalid

XMLWordPrintable

    • sparc
    • solaris_2.5



      Name: saC57035 Date: 01/17/97



      The java.awt.Container.remove(int index) throws different exceptions
      if index is out of bounds. The NullPointerException in this case is
      quite unexpected.

      Here is the example demonstrating the bug:

      ---- Test.java ----------------------
      import java.awt.*;

      public class Test {

      public static void main( String argv[] ) {
       Panel p = new Panel();
       for(int i=-2;i<6;i++)
         try {
           p.remove(i);
         } catch(Exception e) {
           System.out.println("Removing "+i+" : "+e);
         }
      }

      }
      -- The output ----------------
      Removing -2 : java.lang.ArrayIndexOutOfBoundsException: -2
      Removing -1 : java.lang.ArrayIndexOutOfBoundsException: -1
      Removing 0 : java.lang.NullPointerException
      Removing 1 : java.lang.NullPointerException
      Removing 2 : java.lang.NullPointerException
      Removing 3 : java.lang.NullPointerException
      Removing 4 : java.lang.ArrayIndexOutOfBoundsException: 4
      Removing 5 : java.lang.ArrayIndexOutOfBoundsException: 5
      ------------------------------

      ======================================================================



      Modifying the above test as follows:

      java.awt.*;

      public class Test {

          public static void main( String argv[] ) {
               Panel p = new Panel();
               Canvas c;

              for (int i=0;i<100;i++) {
                  c = new Canvas();
                  p.add(c);
              }

             int[] bad={-20, Integer.MIN_VALUE, Integer.MAX_VALUE, 113};
             for(int i = 0; i < bad.length; i++){
                 try {
                   p.remove(bad[i]);
                 } catch(Exception e) {
                   System.out.println("Removing "+ bad[i]+" : "+e);
                 }
             }
          }
      }

      results in the following output <from

      Removing -20 : java.lang.ArrayIndexOutOfBoundsException
      Removing -2147483648 : java.lang.ArrayIndexOutOfBoundsException
      Removing 2147483647 : java.lang.ArrayIndexOutOfBoundsException
      Removing 113 : java.lang.NullPointerException

      Should the removal of the element at position 113 of the container array not result in an ArrayIndexOutOfBoundsException as this position is out of bounds of the array?

      ###@###.### 2001-10-17

            rraysunw Richard Ray (Inactive)
            savzan Stanislav Avzan (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: