Skip to content

Use of var in an enhanced for loop causes NullPointerException #463

Description

@anthonyvdotbe

To reproduce:

  • build ca529a2 with mvn clean verify
  • create Foo.java in core\target:
class Foo {

    void run() {
        for (var arg : List.of()) {}
    }

}
Foo.java:4:15: error: java.lang.NullPointerException
        at com.google.googlejavaformat.java.DimensionHelpers.extractDims(DimensionHelpers.java:109)
        at com.google.googlejavaformat.java.DimensionHelpers.extractDims(DimensionHelpers.java:58)
        at com.google.googlejavaformat.java.JavaInputAstVisitor.visitToDeclare(JavaInputAstVisitor.java:2564)
        at com.google.googlejavaformat.java.JavaInputAstVisitor.visitEnhancedForLoop(JavaInputAstVisitor.java:763)
        at com.google.googlejavaformat.java.JavaInputAstVisitor.visitEnhancedForLoop(JavaInputAstVisitor.java:158)
        at jdk.compiler/com.sun.tools.javac.tree.JCTree$JCEnhancedForLoop.accept(JCTree.java:1190)
        at jdk.compiler/com.sun.source.util.TreePathScanner.scan(TreePathScanner.java:82)
        at com.google.googlejavaformat.java.JavaInputAstVisitor.scan(JavaInputAstVisitor.java:330)
        at com.google.googlejavaformat.java.JavaInputAstVisitor.visitStatements(JavaInputAstVisitor.java:2167)
        at com.google.googlejavaformat.java.JavaInputAstVisitor.methodBody(JavaInputAstVisitor.java:1496)
        at com.google.googlejavaformat.java.JavaInputAstVisitor.visitMethod(JavaInputAstVisitor.java:1483)
        at com.google.googlejavaformat.java.JavaInputAstVisitor.visitMethod(JavaInputAstVisitor.java:158)
        at jdk.compiler/com.sun.tools.javac.tree.JCTree$JCMethodDecl.accept(JCTree.java:916)
        at jdk.compiler/com.sun.source.util.TreePathScanner.scan(TreePathScanner.java:82)
        at com.google.googlejavaformat.java.JavaInputAstVisitor.scan(JavaInputAstVisitor.java:330)
        at com.google.googlejavaformat.java.JavaInputAstVisitor.addBodyDeclarations(JavaInputAstVisitor.java:3510)
        at com.google.googlejavaformat.java.JavaInputAstVisitor.visitClassDeclaration(JavaInputAstVisitor.java:1990)
        at com.google.googlejavaformat.java.java14.Java14InputAstVisitor.visitClass(Java14InputAstVisitor.java:81)
        at com.google.googlejavaformat.java.java14.Java14InputAstVisitor.visitClass(Java14InputAstVisitor.java:45)
        at jdk.compiler/com.sun.tools.javac.tree.JCTree$JCClassDecl.accept(JCTree.java:823)
        at jdk.compiler/com.sun.source.util.TreePathScanner.scan(TreePathScanner.java:82)
        at com.google.googlejavaformat.java.JavaInputAstVisitor.scan(JavaInputAstVisitor.java:330)
        at com.google.googlejavaformat.java.JavaInputAstVisitor.visitCompilationUnit(JavaInputAstVisitor.java:375)
        at com.google.googlejavaformat.java.JavaInputAstVisitor.visitCompilationUnit(JavaInputAstVisitor.java:158)
        at jdk.compiler/com.sun.tools.javac.tree.JCTree$JCCompilationUnit.accept(JCTree.java:603)
        at jdk.compiler/com.sun.source.util.TreePathScanner.scan(TreePathScanner.java:82)
        at com.google.googlejavaformat.java.JavaInputAstVisitor.scan(JavaInputAstVisitor.java:330)
        at com.google.googlejavaformat.java.Formatter.format(Formatter.java:170)
        at com.google.googlejavaformat.java.Formatter.getFormatReplacements(Formatter.java:291)
        at com.google.googlejavaformat.java.Formatter.formatSource(Formatter.java:267)
        at com.google.googlejavaformat.java.FormatFileCallable.call(FormatFileCallable.java:45)
        at com.google.googlejavaformat.java.FormatFileCallable.call(FormatFileCallable.java:26)
        at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
        at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1130)
        at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:630)
        at java.base/java.lang.Thread.run(Thread.java:832)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions