Marek Sabo created MCHECKSTYLE-345:
--------------------------------------

             Summary: Checkstyle crashes on NPE when for loop without braces 
has direct if with braces
                 Key: MCHECKSTYLE-345
                 URL: https://issues.apache.org/jira/browse/MCHECKSTYLE-345
             Project: Maven Checkstyle Plugin
          Issue Type: Bug
    Affects Versions: 2.17
         Environment: ubuntu 16.04, confirmed on windows 10 too
            Reporter: Marek Sabo
         Attachments: issue-NPE-project.zip

Hello,

I would like to inform you that the following code causes checkstyle to crash:

{code:java}
class Demo {
    public static void main(String[] args) {
        for (int i = 0; i < 5; i++)
            if (1 < 2) {
                System.out.println("Multiple line if block");
            }
    }
}
{code}

It happens when an `for` loop without braces contains nested `if` with curly 
braces inside.

$ mvn clean install          

{code:txt}
[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building temp 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ temp ---
[INFO] Deleting /home/mrek/temp/temp/target
[INFO] 
[INFO] --- maven-checkstyle-plugin:2.17:check (validate) @ temp ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.856 s
[INFO] Finished at: 2017-11-26T17:26:49+01:00
[INFO] Final Memory: 12M/195M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal 
org.apache.maven.plugins:maven-checkstyle-plugin:2.17:check (validate) on 
project temp: Execution validate of goal 
org.apache.maven.plugins:maven-checkstyle-plugin:2.17:check failed. 
NullPointerException -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e 
switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please 
read the following articles:
[ERROR] [Help 1] 
http://cwiki.apache.org/confluence/display/MAVEN/PluginExecutionException
{code}


Although I can agree that the code above is not nice at all, it is definitely a 
bug.
I can agree that the best way to avoid it is to to not write code like this at 
all, but our students sometimes write code like this and then they have surely 
no idea why the build is failing.

Please see the simple project in the attachment (just this Demo.java with pom 
and config).
I am also adding output of those files:

config.xml
{code: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">
    <module name="TreeWalker">
        <module name="NeedBraces">
            <property name="allowSingleLineStatement" value="true"/>
        </module>
    </module>
</module>
<?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">

    <module name="TreeWalker">

        <module name="NeedBraces">
            <property name="allowSingleLineStatement" value="true"/>
        </module>
    </module>
</module>
{code}

pom.xml
{code:xml}
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0";
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
http://maven.apache.org/xsd/maven-4.0.0.xsd";>
    <modelVersion>4.0.0</modelVersion>

    <groupId>temp</groupId>
    <artifactId>temp</artifactId>
    <version>1.0-SNAPSHOT</version>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-checkstyle-plugin</artifactId>
                <version>2.17</version>

                <executions>
                    <execution>
                        <id>validate</id>
                        <phase>validate</phase>
                        <configuration>
                            <configLocation>config.xml</configLocation>
                        </configuration>
                        <goals>
                            <goal>check</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>
{code}


I hope it is an easy fix and could be resolved!

Thank you,

Marek



--
This message was sent by Atlassian JIRA
(v6.4.14#64029)

Reply via email to