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

java.awt.Container.remove(int) throws unexpected NPE

    • mantis
    • sparc
    • solaris_2.6



      Name: acR10002 Date: 12/04/2001



      This has been initially reported in 1997 against 1.1 as a bug 4026541.
      The bug 4026541 has been closed as not a bug since the example provided no
      longer exhibits the problem. The problem is that Container.remove(int)
      method can throw different exception under certain conditions. I've found
      that it always throw ArrayIndexOutOfBoundsException except the case then
      the Component has been already added/removed in the past. In the latter
      case NPE is thrown and this is the unexpected behavior. The following
      example can be used to demonstrate the bug still exist in 1.4:

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

      public class Test {
          public static void main( String argv[] ) {
              Panel p=new Panel();
              p.add(new Canvas());
              p.remove(0);

              int[] bad = {-1, 0, 1};
              for (int i=0; i<bad.length; i++)
              try {
                 System.err.println("Removing "+bad[i]);
                 p.remove(bad[i]);
                 System.err.println("No exception");
              } catch(Exception e) {
                 System.err.println(e);
              }
          }
      }
      -------------------------------------------------

      An output under jdk1.4b88 will be:

      ----------- example output ---------

      --> java -version
      java version "1.4.0-rc"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0-rc-b88)
      Java HotSpot(TM) Client VM (build 1.4.0-rc-b88, mixed mode)

      --> java Test
      Removing -1
      java.lang.ArrayIndexOutOfBoundsException
      Removing 0
      java.lang.NullPointerException
      Removing 1
      java.lang.ArrayIndexOutOfBoundsException

      ------------------------------------

      So the NPE is thrown for a position where a component was added and then
      removed in the past.

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

            son Oleg Sukhodolsky (Inactive)
            aycsunw Ayc Ayc (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: