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

Jan Lahoda jlahoda at openjdk.org
Tue Aug 19 07:44:09 UTC 2025


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.

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

Commit messages:
 - Updating copyright year.
 - Improving test
 - 8365689: Elements.getFileObjectOf fails with a NullPointerException when an erroneous Element is passed in

Changes: https://git.openjdk.org/jdk/pull/26836/files
  Webrev: https://webrevs.openjdk.org/?repo=jdk&pr=26836&range=00
  Issue: https://bugs.openjdk.org/browse/JDK-8365689
  Stats: 159 lines in 2 files changed: 156 ins; 0 del; 3 mod
  Patch: https://git.openjdk.org/jdk/pull/26836.diff
  Fetch: git fetch https://git.openjdk.org/jdk.git pull/26836/head:pull/26836

PR: https://git.openjdk.org/jdk/pull/26836


More information about the compiler-dev mailing list