JDK-4546535 : java.awt.Container.remove(int) throws unexpected NPE
  • Type: Bug
  • Component: client-libs
  • Sub-Component: java.awt
  • Affected Version: 1.4.0
  • Priority: P4
  • Status: Resolved
  • Resolution: Fixed
  • OS: solaris_2.6
  • CPU: sparc
  • Submitted: 2001-12-04
  • Updated: 2002-10-18
  • Resolved: 2002-10-18
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
Other
1.4.2 mantisFixed
Related Reports
Relates :  
Relates :  
Description

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.

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

Comments
CONVERTED DATA BugTraq+ Release Management Values COMMIT TO FIX: mantis FIXED IN: mantis INTEGRATED IN: mantis mantis-b04
24-08-2004

SUGGESTED FIX Name: osR10079 Date: 10/04/2002 ------- Container.java ------- *** /tmp/dw3aGFH Fri Oct 4 12:34:35 2002 --- Container.java Fri Oct 4 12:31:16 2002 *************** *** 708,713 **** --- 708,716 ---- */ public void remove(int index) { synchronized (getTreeLock()) { + if (index < 0 || index >= ncomponents) { + throw new ArrayIndexOutOfBoundsException(index); + } Component comp = component[index]; if (peer != null) { comp.removeNotify(); ###@###.### 2002-10-04 ======================================================================
04-10-2002

EVALUATION Compatability problem; committing to hopper, tiger. ###@###.### 2001-12-04 Name: osR10079 Date: 10/04/2002 Current implementation of Container.remove(int) doesn't throw any exceptions itself, but uses exception thrown by underlying calls. To fix the problem we should throw expected exception explicitly. ###@###.### 2002-10-04 ======================================================================
04-10-2002