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

(coll) checked collections should consistently permit nulls

XMLWordPrintable

    • b19
    • x86
    • windows_xp
    • Not verified

      FULL PRODUCT VERSION :


      A DESCRIPTION OF THE PROBLEM :
      You cannot add null to checked collections using add() (and set()) methods. However, nulls can be added through addAll() methods.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      compile and execute attached source code

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      []

      l.add(null);
      java.lang.ClassCastException

      l.addAll(Collections.<String>singletonList(null));
      java.lang.ClassCastException

      ACTUAL -
      []

      l.add(null);
      java.lang.NullPointerException
      at java.util.Collections$CheckedCollection.typeCheck(Collections.java:2205)
      at java.util.Collections$CheckedCollection.add(Collections.java:2246)
      at Main.main(Main.java:9)

      l.addAll(Collections.<String>singletonList(null));
      [null]


      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.util.*;

      public class Main {
      public static void main(String[] args) {
      List<String> l = Collections.checkedList(new ArrayList<String>(), String.class);
      System.out.println(l);
      try {
      System.out.println("\nl.add(null);");
      l.add(null);
      System.out.println(l);
      }
      catch(RuntimeException exc) { // <-- goes here
      exc.printStackTrace(System.out);
      }
      try {
      System.out.println("\nl.addAll(Collections.<String>singletonList(null));");
      l.addAll(Collections.<String>singletonList(null));
      System.out.println(l); // <-- goes here
      }
      catch(RuntimeException exc) {
      exc.printStackTrace(System.out);
      }
      }
      }

      ---------- END SOURCE ----------

            martin Martin Buchholz
            tbell Tim Bell
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: