JDK-8044737 : Lambda: NPE while obtaining method reference through lambda expression
  • Type: Bug
  • Component: tools
  • Sub-Component: javac
  • Affected Version: 8,9
  • Priority: P2
  • Status: Closed
  • Resolution: Fixed
  • Submitted: 2014-06-03
  • Updated: 2016-01-22
  • Resolved: 2014-06-24
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 8 JDK 9
8u40Fixed 9 b22Fixed
Related Reports
Relates :  
Relates :  
Relates :  
Relates :  
Relates :  
Description
Code:

$ cat Test.java

public class Test  {

    interface Constructor {
        MyTest execute();
    }

    class MyTest {
        MyTest() { System.out.println("Constructor executed"); }
    }

    public Constructor getConstructor() {
        return getConstructor(() -> { return MyTest::new; });
    }

    public static void main(String argv[]) {
        Test t = new Test();
        MyTest mytest = t.getConstructor().execute();
    }

    Constructor getConstructor(Wrapper arg) {
        return arg.unwrap();
    }

    interface Wrapper {
        Constructor unwrap();
    }
}

Generates NullPointerException on compilation:

$ javac Test.java 
An exception has occurred in the compiler (1.9.0-internal). Please file a bug at the Java Developer Connection (http://java.sun.com/webapps/bugreport)  after checking the Bug Parade for duplicates. Include your program and the following diagnostic in your report.  Thank you.
java.lang.NullPointerException
	at com.sun.tools.javac.jvm.Code.emitop0(Code.java:563)
	at com.sun.tools.javac.jvm.Items$SelfItem.load(Items.java:367)
	at com.sun.tools.javac.jvm.Gen.genArgs(Gen.java:959)
	at com.sun.tools.javac.jvm.Gen.visitApply(Gen.java:1898)
	at com.sun.tools.javac.tree.JCTree$JCMethodInvocation.accept(JCTree.java:1459)
	at com.sun.tools.javac.jvm.Gen.genExpr(Gen.java:940)
	at com.sun.tools.javac.jvm.Gen.visitReturn(Gen.java:1856)
	at com.sun.tools.javac.tree.JCTree$JCReturn.accept(JCTree.java:1378)
	at com.sun.tools.javac.jvm.Gen.genDef(Gen.java:730)
	at com.sun.tools.javac.jvm.Gen.genStat(Gen.java:765)
	at com.sun.tools.javac.jvm.Gen.genStat(Gen.java:751)
	at com.sun.tools.javac.jvm.Gen.genStats(Gen.java:802)
	at com.sun.tools.javac.jvm.Gen.visitBlock(Gen.java:1151)
	at com.sun.tools.javac.tree.JCTree$JCBlock.accept(JCTree.java:903)
	at com.sun.tools.javac.jvm.Gen.genDef(Gen.java:730)
	at com.sun.tools.javac.jvm.Gen.genStat(Gen.java:765)
	at com.sun.tools.javac.jvm.Gen.genMethod(Gen.java:1024)
	at com.sun.tools.javac.jvm.Gen.visitMethodDef(Gen.java:987)
	at com.sun.tools.javac.tree.JCTree$JCMethodDecl.accept(JCTree.java:772)
	at com.sun.tools.javac.jvm.Gen.genDef(Gen.java:730)
	at com.sun.tools.javac.jvm.Gen.genClass(Gen.java:2518)
	at com.sun.tools.javac.main.JavaCompiler.genCode(JavaCompiler.java:744)
	at com.sun.tools.javac.main.JavaCompiler.generate(JavaCompiler.java:1571)
	at com.sun.tools.javac.main.JavaCompiler.generate(JavaCompiler.java:1535)
	at com.sun.tools.javac.main.JavaCompiler.compile2(JavaCompiler.java:906)
	at com.sun.tools.javac.main.JavaCompiler.compile(JavaCompiler.java:865)
	at com.sun.tools.javac.main.Main.compile(Main.java:521)
	at com.sun.tools.javac.main.Main.compile(Main.java:381)
	at com.sun.tools.javac.main.Main.compile(Main.java:370)
	at com.sun.tools.javac.main.Main.compile(Main.java:361)
	at com.sun.tools.javac.Main.compile(Main.java:56)
	at com.sun.tools.javac.Main.main(Main.java:42)

Changing 'getConstructor()' method to implementation without lambda wrapper works good:

    public Constructor getConstructor() {
        return MyTest::new;
    }

$ javac Test.java
$ java Test 
Constructor executed

Comments
Test(s) was added with this push --- URL: http://hg.openjdk.java.net/jdk9/dev/langtools/rev/2b6b96ed3878 User: rfield Date: 2014-06-25 18:22:47 +0000
27-06-2014

SQE is ok to defer from 8u20.
18-06-2014

Need SQE-OK and recommended release that this would be deferred to before this can be considered for approval.
18-06-2014

8u20-defer-request: 8037404, 8038776, 8044737, and 8044748 form a cluster of issues related to inadequate handling of constructor references to nested classes in method references. They cause crashes of the compiler and/or the running user code, and thus are high impact. They have been reported in at least five cases in fully supported code and thus this is medium likelihood. The workaround is to manually convert them to lambdas which is a low hurdle workaround. Thus ILM of HML. The fix requires rearchitecting of how method references are handled, with hundreds of lines of code change, and thus is inappropriate for an update release. The fix is under review for push to JDK9.
16-06-2014

Possible duplicate
04-06-2014

Probably related to JDK-8037404
04-06-2014