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

Pair class throws a NullPointerException on hashCode method invocation

XMLWordPrintable

    • generic
    • generic

      FULL PRODUCT VERSION :
      java version "1.8.0_121"
      Java(TM) SE Runtime Environment (build 1.8.0_121-b13)
      Java HotSpot(TM) 64-Bit Server VM (build 25.121-b13, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows [Version 6.1.7601]

      A DESCRIPTION OF THE PROBLEM :
      javafx.util.Pair<String, String> class throws a NullPointerException on hashCode () method invocation.

      It happens when key is null.

      This is my test code:
      package zu.luciano;

      import javafx.util.Pair;

      public class PairTest {

      public static void main(String[] args) {
      Pair<String, String> p = new Pair<String, String>(null, "v");
      p.hashCode();
      }

      }

      The bug is on row 97 of Pair class:
      return key.hashCode() * 13 + (value == null ? 0 : value.hashCode());

      Suggested fix is:
      return (key == null ? 0 : key.hashCode()) * 13 + (value == null ? 0 : value.hashCode());

      OR if key == null is not a permitted value add a condition in constructor method.


      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Run this code:

      package zu.luciano;

      import javafx.util.Pair;

      public class PairTest {

      public static void main(String[] args) {
      Pair<String, String> p = new Pair<String, String>(null, "v");
      p.hashCode();
      }

      }

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      an integer as hashcode
      ACTUAL -
      NullPointerException

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      package zu.luciano;

      import javafx.util.Pair;

      public class PairTest {

      public static void main(String[] args) {
      Pair<String, String> p = new Pair<String, String>(null, "v");
      p.hashCode();
      }

      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Check if key is null, of course.

            pmangal Priyanka Mangal (Inactive)
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: