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

NullPointerException with AbstractMap.equals

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P4 P4
    • 1.4.0
    • 1.4.0
    • core-libs
    • beta3
    • generic
    • generic
    • Verified



      Name: ddT132432 Date: 09/17/2001


      java version "1.4.0-beta2"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0-beta2-b77)
      Java HotSpot(TM) Client VM (build 1.4.0-beta2-b77, mixed mode)


      The general contract of Map.equals() does not permit throwing a
      NullPointerException; it simply permits returning true or false. However, this
      simple program demonstrates that AbstractMap simply delegates to Map.get(),
      which can throw a NullPointerException in the case of Hashtable, where null keys
      are not permitted. Therefore, AbstractMap is flawed, and needs to trap any
      exceptions and return false in such a case.

      import java.util.*;
      class Foo {
        public static void main(String[] args) {
          Map m = new HashMap();
          m.put(null, "");
          Map h = new Hashtable();
          h.put("", "");
          m.equals(h);
        }
      }

      $ java Foo
      Exception in thread "main" java.lang.NullPointerException
              at java.util.Hashtable.get(Hashtable.java:321)
              at java.util.AbstractMap.equals(AbstractMap.java:519)
              at Foo.main(Foo.java:8)
      (Review ID: 132020)
      ======================================================================

            jjb Josh Bloch (Inactive)
            ddressersunw Daniel Dresser (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: