JDK-8132117 : NullPointerException from method reference on null object
  • Type: Bug
  • Component: tools
  • Sub-Component: javac
  • Affected Version: 8u45
  • Priority: P4
  • Status: Closed
  • Resolution: Not an Issue
  • OS: windows_7
  • CPU: x86_64
  • Submitted: 2015-07-22
  • Updated: 2015-07-27
  • Resolved: 2015-07-27
Related Reports
Relates :  
Description
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

Comments
Not an issue. See JDK-8131323 for more detailed explanation including JLS text.
27-07-2015