Skip to content

Null pointer Exception in UnusedLocalVariableCheck #13167

@Kevin222004

Description

@Kevin222004

I have read check documentation: https://checkstyle.org/config_coding.html#UnusedLocalVariable
I have downloaded the latest checkstyle from: https://checkstyle.org/cmdline.html#Download_and_Run
I have executed the cli and showed it below, as cli describes the problem better than 1,000 words

kevin@inspiron-15-5510:~/Desktop/check_style$ cat Test.java
import javax.swing.*;
import java.awt.*;
import java.util.function.Supplier;

public class Test {
   private final NotNullLazyValue<JComponent> myComponent = NotNullLazyValue.lazy(() -> {
      JPanel results = new JPanel(new GridBagLayout()) {};
       return results;
   });
}
class NotNullLazyValue<T> {
    public NotNullLazyValue() {
    }
    public static <T> NotNullLazyValue<T> lazy(Supplier<T> supplier) {
        return new NotNullLazyValue<>();
    }
}

kevin@inspiron-15-5510:~/Desktop/check_style$ javac Test.java

kevin@inspiron-15-5510:~/Desktop/check_style$ cat config.xml 
<?xml version="1.0"?>
<!DOCTYPE module PUBLIC "-//Checkstyle//DTD Checkstyle Configuration 1.3//EN" "https://checkstyle.org/dtds/configuration_1_3.dtd">

<module name="Checker">
    <property name="charset" value="UTF-8" />
    <property name="severity" value="warning" />
    <property name="haltOnException" value="false" />

    <module name="TreeWalker">

        <module name="UnusedLocalVariable">
        </module>

    </module>
</module>

kevin@inspiron-15-5510:~/Desktop/check_style$ java -jar /home/kevin/Downloads/checkstyle-10.12.0-all.jar -c config.xml Test.java
Starting audit...
[ERROR] /home/kevin/Desktop/check_style/Test.java:1: Got an exception - java.lang.NullPointerException: Cannot invoke "com.puppycrawl.tools.checkstyle.api.DetailAST.getType()" because "parentAst" is null
        at com.puppycrawl.tools.checkstyle.checks.coding.UnusedLocalVariableCheck.getBlockContainingLocalAnonInnerClass(UnusedLocalVariableCheck.java:480)
        at com.puppycrawl.tools.checkstyle.checks.coding.UnusedLocalVariableCheck.visitLocalAnonInnerClass(UnusedLocalVariableCheck.java:404)
        at com.puppycrawl.tools.checkstyle.checks.coding.UnusedLocalVariableCheck.visitToken(UnusedLocalVariableCheck.java:312)
        at com.puppycrawl.tools.checkstyle.TreeWalker.notifyVisit(TreeWalker.java:335)
        at com.puppycrawl.tools.checkstyle.TreeWalker.processIter(TreeWalker.java:406)
        at com.puppycrawl.tools.checkstyle.TreeWalker.walk(TreeWalker.java:273)
        at com.puppycrawl.tools.checkstyle.TreeWalker.processFiltered(TreeWalker.java:154)
        at com.puppycrawl.tools.checkstyle.api.AbstractFileSetCheck.process(AbstractFileSetCheck.java:98)
        at com.puppycrawl.tools.checkstyle.Checker.processFile(Checker.java:334)
        at com.puppycrawl.tools.checkstyle.Checker.processFiles(Checker.java:293)
        at com.puppycrawl.tools.checkstyle.Checker.process(Checker.java:223)
        at com.puppycrawl.tools.checkstyle.Main.runCheckstyle(Main.java:415)
        at com.puppycrawl.tools.checkstyle.Main.runCli(Main.java:338)
        at com.puppycrawl.tools.checkstyle.Main.execute(Main.java:195)
        at com.puppycrawl.tools.checkstyle.Main.main(Main.java:130)
 [Checker]
Audit done.
Checkstyle ends with 1 errors.
kevin@inspiron-15-5510:~/Desktop/check_style$ 

Describe what you expect in detail.

Should Not throw Null pointer Exception


Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions