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

(1.1) java.util.Vector toString() method throws NullPointerException on null ele

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P4 P4
    • 1.2.0
    • 1.1.8
    • core-libs



      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)
      ======================================================================

            mmcclosksunw Michael Mccloskey (Inactive)
            vasya Vassili Igouchkine (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: