NullPointerException when using NeedBraces with tokens LITERAL_CASE, LITERAL_DEFAULT #3655

Closed
jokarls opened this Issue Dec 14, 2016 · 2 comments

Comments

Projects
None yet
2 participants
@jokarls

jokarls commented Dec 14, 2016

/var/tmp $ javac Test.java

/var/tmp $ cat Test.java

import java.lang.annotation.ElementType;

public class Test {

    private static void add(ElementType limit) {
        String value = "";
        switch (limit) {
            case FIELD:
                value = "FIELD";
                break;
            case METHOD:
                value = "METHOD";
                break;
            default:
        }
    }
}

/var/tmp $ cat checkstyle.xml

<?xml version="1.0"?>
<!DOCTYPE module PUBLIC
        "-//Puppy Crawl//DTD Check Configuration 1.3//EN"
        "http://www.puppycrawl.com/dtds/configuration_1_3.dtd">

<module name = "Checker">
    <property name="charset" value="UTF-8"/>
    <module name="TreeWalker">
        <module name="NeedBraces">
            <!--  This breaks the build for some reason -->
            <property name="tokens" value="LITERAL_CASE, LITERAL_DEFAULT"/>
            <property name="allowSingleLineStatement" value="true"/>
            <property name="allowEmptyLoopBody" value="true"/>
        </module>
    </module>
</module>

/var/tmp $ java -jar checkstyle-7.3-all.jar -c checkstyle.xml Test.java
Starting audit...
com.puppycrawl.tools.checkstyle.api.CheckstyleException: Exception was thrown while processing Test.java
	at com.puppycrawl.tools.checkstyle.Checker.processFiles(Checker.java:287)
	at com.puppycrawl.tools.checkstyle.Checker.process(Checker.java:205)
	at com.puppycrawl.tools.checkstyle.Main.runCheckstyle(Main.java:425)
	at com.puppycrawl.tools.checkstyle.Main.runCli(Main.java:359)
	at com.puppycrawl.tools.checkstyle.Main.main(Main.java:174)
Caused by: java.lang.NullPointerException
	at com.puppycrawl.tools.checkstyle.checks.blocks.NeedBracesCheck.isSingleLineDefault(NeedBracesCheck.java:445)
	at com.puppycrawl.tools.checkstyle.checks.blocks.NeedBracesCheck.isSingleLineStatement(NeedBracesCheck.java:289)
	at com.puppycrawl.tools.checkstyle.checks.blocks.NeedBracesCheck.isSkipStatement(NeedBracesCheck.java:222)
	at com.puppycrawl.tools.checkstyle.checks.blocks.NeedBracesCheck.visitToken(NeedBracesCheck.java:208)
	at com.puppycrawl.tools.checkstyle.TreeWalker.notifyVisit(TreeWalker.java:361)
	at com.puppycrawl.tools.checkstyle.TreeWalker.processIter(TreeWalker.java:498)
	at com.puppycrawl.tools.checkstyle.TreeWalker.walk(TreeWalker.java:303)
	at com.puppycrawl.tools.checkstyle.TreeWalker.processFiltered(TreeWalker.java:178)
	at com.puppycrawl.tools.checkstyle.api.AbstractFileSetCheck.process(AbstractFileSetCheck.java:78)
	at com.puppycrawl.tools.checkstyle.Checker.processFile(Checker.java:307)
	at com.puppycrawl.tools.checkstyle.Checker.processFiles(Checker.java:278)
	... 4 more
Checkstyle ends with 1 errors.

When i add <property name="tokens" value="LITERAL_CASE, LITERAL_DEFAULT"/> to the NeedBraces tag the check fails with NullPointerException


Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.

@romani

This comment has been minimized.

Show comment
Hide comment
@romani

romani Dec 14, 2016

Member

We missed this exception by our ci because we test base on default tokens, not all accepted.
Ci config need to be changed too.

Member

romani commented Dec 14, 2016

We missed this exception by our ci because we test base on default tokens, not all accepted.
Ci config need to be changed too.

liscju added a commit to liscju/checkstyle that referenced this issue Dec 21, 2016

Issue #3655: Fix NPE in NeedBraces on single line default stmt
Problem was that isSingleLineDefault assumed that default stmt
has slist. This commit fixes this by checking if slist exists.

liscju added a commit to liscju/checkstyle that referenced this issue Dec 21, 2016

liscju added a commit to liscju/checkstyle that referenced this issue Dec 21, 2016

romani added a commit that referenced this issue Dec 31, 2016

romani added a commit that referenced this issue Dec 31, 2016

@romani romani added this to the 7.4 milestone Dec 31, 2016

@romani romani closed this Dec 31, 2016

@romani

This comment has been minimized.

Show comment
Hide comment
@romani

romani Dec 31, 2016

Member

fix is merged

Member

romani commented Dec 31, 2016

fix is merged

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