-
Bug
-
Resolution: Duplicate
-
P3
-
None
-
1.0-b21, 1.0-b22
-
generic, sparc
-
linux, solaris_7
======== This is a new description - see below for the old one =========
It's unclear what should happen when removing a component at an invalid index.
It used to be inconsistent before: JDK 1.4.0 used to throw NullPointerException
in some cases and ArrayIndexOutOfBoundsException in others.
Bug 4546535 was fixed in J2SE to always throw ArrayIndexOutOfBoundsException
(I think). Unfortunately, the same bug was fixed to throw IllegalArgumentException
in J2ME PP (see the suggested fix - it was my proposal, sorry). It would be
good to resolve this inconsistency.
###@###.### 2004-10-01
================= This is the old description ==========================
Name: atR10191 Date: 01/28/2002
(it's java bug 4546535 filed against ri in accordance with agreement
with ri team)
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:
----------------- TesX.java ---------------------
import java.awt.*;
import java.util.*;
public class TesX {
public static void main( String argv[] ) {
Container p=new Container();
p.add(new Container());
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 pbp-b22/pp-b42 will be:
----------- example output ---------
Removing -1
java.lang.ArrayIndexOutOfBoundsException
Removing 0
java.lang.NullPointerException
Removing 1
java.lang.NullPointerException
------------------------------------
So the NPE is thrown for a position where a component was added and then
removed in the past.
======================================================================
The addition/removal of the new Container does not affect the behaviour of the test.
When the Container is instantiated, the default constructor creates a Container with a default array length of 4 null components.
Hence, attempts to remove a component from the Container at index < 0 or > 3 will result in an ArrayIndexOutOfBoundsException.
Attempts to remove a component from the Container at index >= 0 or <= 3, will result in a NullPointerException if no component exists within that index of the component array.
However, the exception thrown (if any) should be consistent across all implementations. Hence, it is not acceptable for the NullPointerException to be thrown as the RI has a default creation of an empty 4-element component array. Another implementation might create an empty 5-element array that would result in different behaviour with a different exception thrown.
Hence, a suggested check for the remove(int index) method should:
1. Check if the Container actually contains any components before attempting to remove it.
2. If so, check that the index references an element of the array that actually contains a component.
I would suggest that the spec specify that an ArrayOutOfBoundsException should be thrown if neither criteria are met.
###@###.### 2002-01-29
======================================================================
Changing category to specification so as to determine what exception should be thrown in this case - if any.
It seems that the specification should define the behaviour of a call to Container.remove(int index) with an invalid index.
At present, the specification does not define this behaviour - indicating that the method should fail silently. Would it be more helpful to the user to have this behaviour defined in order to indicate where the error has occurred?
With that in mind, should the specification document that this method can result in an unchecked runtime exception in the @throws tag of this method definition when called with an invalid index - eg. IndexOutOfBoundsException.
Note - this is also an issue for the Personal Profile Spec.
###@###.### 2002-02-20
================================================================================
It's unclear what should happen when removing a component at an invalid index.
It used to be inconsistent before: JDK 1.4.0 used to throw NullPointerException
in some cases and ArrayIndexOutOfBoundsException in others.
Bug 4546535 was fixed in J2SE to always throw ArrayIndexOutOfBoundsException
(I think). Unfortunately, the same bug was fixed to throw IllegalArgumentException
in J2ME PP (see the suggested fix - it was my proposal, sorry). It would be
good to resolve this inconsistency.
###@###.### 2004-10-01
================= This is the old description ==========================
Name: atR10191 Date: 01/28/2002
(it's java bug 4546535 filed against ri in accordance with agreement
with ri team)
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:
----------------- TesX.java ---------------------
import java.awt.*;
import java.util.*;
public class TesX {
public static void main( String argv[] ) {
Container p=new Container();
p.add(new Container());
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 pbp-b22/pp-b42 will be:
----------- example output ---------
Removing -1
java.lang.ArrayIndexOutOfBoundsException
Removing 0
java.lang.NullPointerException
Removing 1
java.lang.NullPointerException
------------------------------------
So the NPE is thrown for a position where a component was added and then
removed in the past.
======================================================================
The addition/removal of the new Container does not affect the behaviour of the test.
When the Container is instantiated, the default constructor creates a Container with a default array length of 4 null components.
Hence, attempts to remove a component from the Container at index < 0 or > 3 will result in an ArrayIndexOutOfBoundsException.
Attempts to remove a component from the Container at index >= 0 or <= 3, will result in a NullPointerException if no component exists within that index of the component array.
However, the exception thrown (if any) should be consistent across all implementations. Hence, it is not acceptable for the NullPointerException to be thrown as the RI has a default creation of an empty 4-element component array. Another implementation might create an empty 5-element array that would result in different behaviour with a different exception thrown.
Hence, a suggested check for the remove(int index) method should:
1. Check if the Container actually contains any components before attempting to remove it.
2. If so, check that the index references an element of the array that actually contains a component.
I would suggest that the spec specify that an ArrayOutOfBoundsException should be thrown if neither criteria are met.
###@###.### 2002-01-29
======================================================================
Changing category to specification so as to determine what exception should be thrown in this case - if any.
It seems that the specification should define the behaviour of a call to Container.remove(int index) with an invalid index.
At present, the specification does not define this behaviour - indicating that the method should fail silently. Would it be more helpful to the user to have this behaviour defined in order to indicate where the error has occurred?
With that in mind, should the specification document that this method can result in an unchecked runtime exception in the @throws tag of this method definition when called with an invalid index - eg. IndexOutOfBoundsException.
Note - this is also an issue for the Personal Profile Spec.
###@###.### 2002-02-20
================================================================================
- duplicates
-
JDK-4647973 spec for Container.remove() incomplete
-
- Resolved
-
- relates to
-
JDK-4546535 java.awt.Container.remove(int) throws unexpected NPE
-
- Resolved
-