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

NullPointerException from method reference on null object

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Not an Issue
    • Icon: P4 P4
    • None
    • 8u45
    • tools
    • x86_64
    • windows_7

      FULL PRODUCT VERSION :

      java version "1.8.0_45"
      Java(TM) SE Runtime Environment (build 1.8.0_45-b15)
      Java HotSpot(TM) 64-Bit Server VM (build 25.45-b02, mixed mode)


      A DESCRIPTION OF THE PROBLEM :

      The following simple example code leads to a NullPointerException:

      import java.util.function.Supplier;

      public class MethodReferenceNpeTest {

      public static void main(String[] args) {
      Object object = createObject();
      Supplier<String> supplier = object::toString;
      if (object != null) {
      System.out.println(supplier.get());
      }
      }

      private static Object createObject() {
      return null;
      }

      }

      RESULT :

      After compiling it with javac and running it, following NPE occurs:

      Exception in thread "main" java.lang.NullPointerException
      at de.fhg.iwes.ui.vpp.MethodReferenceNpeTest.main(MethodReferenceNpeTest.java:9)

      -- Compiling the same code with the eclipse compiler doesn't lead to a NPE

            mcimadamore Maurizio Cimadamore
            aliebelt Andreas Liebelt
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: