RFR: 8365689: Elements.getFileObjectOf fails with a NullPointerException when an erroneous Element is passed in

Joe Darcy darcy at openjdk.org
Tue Aug 26 01:57:34 UTC 2025


On Tue, 19 Aug 2025 07:35:05 GMT, Jan Lahoda <jlahoda at openjdk.org> wrote:

> If an erroneous/unresolvable `Element` is passed to `Elements.getFileObjectOf`, it fails with an NPE like:
> 
> Caused by: java.lang.NullPointerException: Cannot read field "classfile" because the return value of "com.sun.tools.javac.code.Symbol.enclClass()" is null
> at jdk.compiler/com.sun.tools.javac.model.JavacElements.getFileObjectOf(JavacElements.java:803)
> at TestFileObjectOf$PrintFiles.handleDeclaration(TestFileObjectOf.java:329)
> at jdk.compiler/com.sun.tools.javac.tree.JCTree$JCIdent.accept(JCTree.java:2718)
> 
> 
> The reason is the `Symbol.enclClass()` will return `null` for the erroneous `Element`. The proposed solution herein is to return `null` for erroneous/unresolvable `Element`s.

The updated behavior is consistent with the specification of the method.

-------------

PR Comment: https://git.openjdk.org/jdk/pull/26836#issuecomment-3222287944


More information about the compiler-dev mailing list