JDK-8372050 : Javac NPE Crash - Local class constructor reference in static context & closure
  • Type: Bug
  • Component: tools
  • Sub-Component: javac
  • Affected Version: 16,23
  • Priority: P3
  • Status: Closed
  • Resolution: Duplicate
  • OS: generic
  • CPU: generic
  • Submitted: 2025-11-17
  • Updated: 2025-12-01
  • Resolved: 2025-11-20
Related Reports
Causes :  
Duplicate :  
Description
ADDITIONAL SYSTEM INFORMATION :
Ubuntu 24.04.3 LTS 
Kernel version 6.14.0-35-generic
javac 21.0.9

A DESCRIPTION OF THE PROBLEM :
Compiler is not accusing illegal use of local class constructor in a static context when using method reference. The compiler fails with a NPE crash if this happens within a closure.
This is related to https://bugs.openjdk.org/browse/JDK-8333313, and fix was confirmed in JDK 24

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Compile the provided source code


EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Javac reports a compile error when trying to reference a constructor of a Local class in a static context, such as:
```
error: local class Local cannot be instantiated from a static context
```
ACTUAL -
Javac crashes with the exception:
```
An exception has occurred in the compiler (21.0.9). Please file a bug against the Java compiler via the Java bug reporting page (https://bugreport.java.com) after checking the Bug Database (https://bugs.java.com) for duplicates. Include your program, the following diagnostic, and the parameters passed to the Java compiler in your report. Thank you.
java.lang.NullPointerException: Cannot read field "sym" because "this.lvar[0]" is null
	at jdk.compiler/com.sun.tools.javac.jvm.Code.emitop0(Code.java:568)
	at jdk.compiler/com.sun.tools.javac.jvm.Items$SelfItem.load(Items.java:369)
	at jdk.compiler/com.sun.tools.javac.jvm.Gen.visitIdent(Gen.java:2368)
	at jdk.compiler/com.sun.tools.javac.tree.JCTree$JCIdent.accept(JCTree.java:2715)
	at jdk.compiler/com.sun.tools.javac.jvm.Gen.genExpr(Gen.java:885)
	at jdk.compiler/com.sun.tools.javac.jvm.Gen.genArgs(Gen.java:910)
	at jdk.compiler/com.sun.tools.javac.jvm.Gen.visitNewClass(Gen.java:2053)
	at jdk.compiler/com.sun.tools.javac.tree.JCTree$JCNewClass.accept(JCTree.java:1871)
	at jdk.compiler/com.sun.tools.javac.jvm.Gen.genExpr(Gen.java:885)
	at jdk.compiler/com.sun.tools.javac.jvm.Gen.visitReturn(Gen.java:1909)
	at jdk.compiler/com.sun.tools.javac.tree.JCTree$JCReturn.accept(JCTree.java:1728)
	at jdk.compiler/com.sun.tools.javac.jvm.Gen.genDef(Gen.java:614)
	at jdk.compiler/com.sun.tools.javac.jvm.Gen.genStat(Gen.java:649)
	at jdk.compiler/com.sun.tools.javac.jvm.Gen.genStat(Gen.java:635)
	at jdk.compiler/com.sun.tools.javac.jvm.Gen.genStats(Gen.java:686)
	at jdk.compiler/com.sun.tools.javac.jvm.Gen.doVisitBlock(Gen.java:1138)
	at jdk.compiler/com.sun.tools.javac.jvm.Gen.visitBlock(Gen.java:1117)
	at jdk.compiler/com.sun.tools.javac.tree.JCTree$JCBlock.accept(JCTree.java:1092)
	at jdk.compiler/com.sun.tools.javac.jvm.Gen.genDef(Gen.java:614)
	at jdk.compiler/com.sun.tools.javac.jvm.Gen.genStat(Gen.java:649)
	at jdk.compiler/com.sun.tools.javac.jvm.Gen.genMethod(Gen.java:975)
	at jdk.compiler/com.sun.tools.javac.jvm.Gen.visitMethodDef(Gen.java:938)
	at jdk.compiler/com.sun.tools.javac.tree.JCTree$JCMethodDecl.accept(JCTree.java:916)
	at jdk.compiler/com.sun.tools.javac.jvm.Gen.genDef(Gen.java:614)
	at jdk.compiler/com.sun.tools.javac.jvm.Gen.genClass(Gen.java:2506)
	at jdk.compiler/com.sun.tools.javac.main.JavaCompiler.genCode(JavaCompiler.java:769)
	at jdk.compiler/com.sun.tools.javac.main.JavaCompiler.generate(JavaCompiler.java:1710)
	at jdk.compiler/com.sun.tools.javac.main.JavaCompiler.generate(JavaCompiler.java:1678)
	at jdk.compiler/com.sun.tools.javac.main.JavaCompiler.compile(JavaCompiler.java:976)
	at jdk.compiler/com.sun.tools.javac.main.Main.compile(Main.java:319)
	at jdk.compiler/com.sun.tools.javac.main.Main.compile(Main.java:178)
	at jdk.compiler/com.sun.tools.javac.Main.compile(Main.java:64)
	at jdk.compiler/com.sun.tools.javac.Main.main(Main.java:50)
```

---------- BEGIN SOURCE ----------
import java.util.function.Supplier;

class TestCaseJavac21NpeCrash {
    public static void main(final String[] args) {
        class Local {
            static Supplier<Local> staticFunction() {
                return Local::new;
            }

            public String[] closure() {
                return args;
            }
        }
    }
}
---------- END SOURCE ----------


Comments
[~vromero] Please note that issues closed as `Duplicate` should be `Closed`, not `Resolved`. Please clean this up. See https://openjdk.org/guide/#resolving-or-closing-an-issue for more information.
24-11-2025

This is a backport request, as this duplicates JDK-8322882. This should be closed as duplicate no matter if the actual fix is backported or not.
18-11-2025

The observations on Windows 11: JDK 16ea+25: Passed, no crash error JDK 16ea+26: Failed, javac crashed JDK 24ea+24: Failed. JDK 24ea+25: Passed. JDK 26ea+17: Passed. Impact ->H (Regression) Likelihood -> L (Probably fixed in JDK-8322882) Workaround -> M (Somewhere in-between the extremes) Priority -> P3
18-11-2025