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

Passing a null array to Arrays.asList() doesn't return null or throw Exception

    XMLWordPrintable

Details

    • kestrel
    • generic, x86, sparc
    • generic, solaris_2.6, windows_95

    Description



      Name: jb33418 Date: 11/12/98


      /*
       * If you pass a null pointer to Arrays.asList(),
       * rather than return a null value, it returns a List
       * that fails at a later point when you call any of
       * the methods on it. This can be tricky to debug.
       *
       * I think it should return null (a list view of a
       * null array is a null value, right?) but it could
       * possibly throw an exception or create an array of
       * 0 elements. What it should not do is fail at a
       * later point.
       *
       */
      import java.util.*;

      public class ArrayFail {
          public static void main(String[] args) throws Exception {
              //Create a null array
              Object[] nullArray = null;
              
              //Pass it to Arrays.asList()
              List nullList = Arrays.asList(nullArray);
              
              //My expectation is that nullList should be null
              //But . . .
              System.out.println("Is the list null? " + (nullList == null));
              
              //And, accessing a method on it . . .
              int thisFails = nullList.size();
          }
      }
      (Review ID: 39130)
      ======================================================================

      Attachments

        Issue Links

          Activity

            People

              jjb Josh Bloch
              jjb Josh Bloch
              Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:
                Imported:
                Indexed: