-
Bug
-
Resolution: Fixed
-
P4
-
1.2.0, 1.3.0
-
beta
-
x86
-
windows_95, windows_nt
Name: clC74495 Date: 09/08/98
JComboBox.java:
public int getSelectedIndex() {
Object sObject = dataModel.getSelectedItem();
int i,c;
Object obj;
for ( i=0,c=dataModel.getSize();i<c;i++ ) {
obj = dataModel.getElementAt(i);
if ( obj.equals(sObject) )
return i;
}
return -1;
}
PROBLEM: this will not work if
"dataModel.getElementAt(i)" returns a null
My dataModel includes a null object so that
the user can clear their selection.
There's probably other classes that are also
working this way, but I haven't looked into it.
(Review ID: 38458)
======================================================================
Name: skT45625 Date: 05/18/2000
java version "1.3.0"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.0-C)
Java HotSpot(TM) Client VM (build 1.3.0-C, mixed mode)
Nulls in JComboBox's underlying collection cause lots of trouble.
Also, the code in JComboBox.getSelectedIndex() tests for equality backwards
(I know some people think .equals() should be commutative,
but let's face it: it often isn't and it would be a problem if it was).
In JComboBox.getSelectedIndex(), try this instead:
public int getSelectedIndex()
{
Object o0 = dataModel.getSelectedItem();
if (null != o0)
{
Object o1 = null;
for (int i = 0, z = dataModel.getSize(); i < z; i += 1)
{
o1 = dataModel.getElementAt(i);
if (o0 == o1 || null != o1 && o0.equals(o1))
return i;
}
}
else
for (int i = 0, z = dataModel.getSize(); i < z; i += 1)
if (null == dataModel.getElementAt(i))
return i;
return -1;
}
There may be other related problems with this class.
This was the first apparent problem with null elements under a JComboBox.
(Review ID: 105069)
======================================================================