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

Allow enhanced-for loop over a null array to execute zero times instead of throwing NPE

    Details

    • Type: Enhancement
    • Status: Closed
    • Priority: P4
    • Resolution: Won't Fix
    • Affects Version/s: 14
    • Fix Version/s: None
    • Component/s: specification
    • Labels:

      Description

      A DESCRIPTION OF THE PROBLEM :
      Using a for-each loop over an array is a nasty business :-) that requires an explicit null-check before entering the loop. Suggestion: allow the for-each loop/iterator to detect a null array, and execute 0 times w/o throwing an NPE. E.g.

      String [] none = null;
      for (String some: none) System.err.println(some);

      would execute 0 times, and NOT throw an NPE.

      The only downside to this that I can see is it MIGHT break code that (for poor design reasons) explicitly catches and checks for this case, and does something else (besides executing the loop 0 times). Arrays are passe', to be sure, and doing null-safe List's or Collection's in for-each loops are trivial. But arrays are a pain, and much legacy code (e.g. Castor XML->Java code generators) still produce arrays.


        Attachments

          Activity

            People

            • Assignee:
              Unassigned
              Reporter:
              webbuggrp Webbug Group
            • Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: