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

get(null) on single-entry unmodifiable Map returns null instead of throwing NPE

    XMLWordPrintable

Details

    • CSR
    • Resolution: Approved
    • P3
    • 13
    • core-libs
    • None
    • behavioral
    • low
    • Hide
      It's possible that some code is inadvertently calling get(null) and handling a null return value. This code will probably break if NPE is thrown instead. It's unlikely that such code is relying on this behavior in a fundamental sense, though; it's most likely an accidental dependency and should be easy to find and fix. It's also likely that such code is fragile, in that if number of mappings were to change, NPE would be thrown anyway. Such code will probably have to be fixed sooner or later anyway.
      Show
      It's possible that some code is inadvertently calling get(null) and handling a null return value. This code will probably break if NPE is thrown instead. It's unlikely that such code is relying on this behavior in a fundamental sense, though; it's most likely an accidental dependency and should be easy to find and fix. It's also likely that such code is fragile, in that if number of mappings were to change, NPE would be thrown anyway. Such code will probably have to be fixed sooner or later anyway.
    • Java API
    • Implementation

    Description

      Summary

      An unmodifiable Map with one entry uses a different implementation that behaves inconsistently compared to those with zero or two or more entries. It is also inconsistent with other Map implementations that disallow nulls.

      Problem

      Most operations on unmodifiable collections that involve null will throw NullPointerException. In particular, get(null) on an unmodifiable map with zero or two or more entries will throw NPE. On unmodifiable maps of all sizes, containsKey(null) and containsValue(null) throw NPE, and unmodifiable Lists and Sets throw NPE in response to contains(null). Other map implementations that disallow nulls, such as ConcurrentHashMap and Hashtable, throw NPE for all of these cases. The reason is that nulls are disallowed in these collections. Calling get(null) or contains*(null) is likely to be a programming error. If such methods were to return normally instead of throwing, it might let the error go undetected for longer.

      Calling get(null) on a one-entry unmodifiable map returns null. This is clearly inconsistent with all of the above behaviors.

      Solution

      The one-entry unmodifiable Map implementation should be changed to throw NPE in response to get(null).

      Specification

      No specification change; this is a behavioral change only.

      Attachments

        Issue Links

          Activity

            People

              smarks Stuart Marks
              smarks Stuart Marks
              Brian Burkhalter, Lance Andersen
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: