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

java.lang.NullPointerException when lambda replaced by method reference

XMLWordPrintable

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

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

      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows [Version 6.3.9600]

      A DESCRIPTION OF THE PROBLEM :
      If we replace lambda by method reference, than NPE thrown

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Run

      public class JdkTest {
          private StoreView storeView;

          class ConfigItem<T> {
              private final Supplier<T> getter;

              public ConfigItem(final Supplier<T> getter) {
                  this.getter = getter;
              }
          }

          private ConfigItem<String> supportUser = new ConfigItem<>(() -> storeView.get());
          private ConfigItem<String> supportUser2 = new ConfigItem<>(storeView::get);

          private class StoreView {
              public String get() {
                  return "123";
              }
          }

          public static void main(String[] args) {
              new JdkTest();
          }
      }

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      successfully finished
      ACTUAL -
      NullPointerException

      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      Exception in thread "main" java.lang.NullPointerException
      at com.muranosoft.wrike.persistence.service.zendesk.JdkTest.<init>(JdkTest.java:15)
      at com.muranosoft.wrike.persistence.service.zendesk.JdkTest.main(JdkTest.java:36)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:497)
      at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      public class JdkTest {
          private StoreView storeView;

          class ConfigItem<T> {
              private final Supplier<T> getter;

              public ConfigItem(final Supplier<T> getter) {
                  this.getter = getter;
              }
          }

          private ConfigItem<String> supportUser = new ConfigItem<>(() -> storeView.get());
          private ConfigItem<String> supportUser2 = new ConfigItem<>(storeView::get);

          private class StoreView {
              public String get() {
                  return "123";
              }
          }

          public static void main(String[] args) {
              new JdkTest();
          }
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
          private StoreView storeView = new StoreView();


            psandoz Paul Sandoz
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: