-
Bug
-
Resolution: Fixed
-
P4
-
1.1.8
-
1.2
-
x86
-
windows_nt
Name: vi73552 Date: 05/21/99
Vector toString() method throws NullPointerException
on null element. Vectors can contain null elements,
as well as Objects, so toString() should not blow up
like this.
Additionally, the toString() method is declared
public final synchronized String toString()
so it cannot be overridden and fixed in a local subclass of Vector.
----------------------------------------
Here is some simple source from a class I'll call VectorNull that easily
reproduces the problem.
The problem is a NullPointerException gets thrown. You know what that is,
right? You don't need me to give you a stack trace or exact text.
package VectorNull;
import java.lang.*;
import java.util.*;
public class VectorNull {
/** constructor */
public VectorNull() {
}
public static void main(String args[]) {
Vector v = new Vector();
v.addElement(null);
System.out.println("Vector v = " + v);
}
}
Here is the reason why it happens. The following code is from your
JDK1.1.7b java.util.Vector toString() method. I don't see a fix described
in the JDK1.1.8 release notes, so I'll assume it's there as well.
/**
* Returns a string representation of this vector.
*
* @return a string representation of this vector.
* @since JDK1.0
*/
public final synchronized String toString() {
int max = size() - 1;
StringBuffer buf = new StringBuffer();
Enumeration e = elements();
buf.append("[");
for (int i = 0 ; i <= max ; i++) {
String s = e.nextElement().toString(); // <------ Here
is the bug! No checking for null for e.nextElement() before invoking
toString()
buf.append(s);
if (i < max) {
buf.append(", ");
}
}
buf.append("]");
return buf.toString();
}
}
(Review ID: 63202)
======================================================================