Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NoWhitespaceAfterCheck for some array initialization crashes Checkstyle #4557

Closed
denizarsan opened this issue Jun 29, 2017 · 13 comments
Closed
Milestone

Comments

@denizarsan
Copy link

denizarsan commented Jun 29, 2017

$ javac C.java

$ cat C.java

class C {
    int i = new int[]{1}[0];
}

$ cat config.xml

<!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="NoWhitespaceAfter"/>
  </module>
</module>

$ java -jar checkstyle-7.8.2-all.jar -c config.xml C.java

Starting audit...
com.puppycrawl.tools.checkstyle.api.CheckstyleException: Exception was thrown while processing C.java
        at com.puppycrawl.tools.checkstyle.Checker.processFiles(Checker.java:295)
        at com.puppycrawl.tools.checkstyle.Checker.process(Checker.java:213)
        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.whitespace.NoWhitespaceAfterCheck.getPositionAfter(NoWhitespaceAfterCheck.java:175)
        at com.puppycrawl.tools.checkstyle.checks.whitespace.NoWhitespaceAfterCheck.visitToken(NoWhitespaceAfterCheck.java:133)
        at com.puppycrawl.tools.checkstyle.TreeWalker.notifyVisit(TreeWalker.java:370)
        at com.puppycrawl.tools.checkstyle.TreeWalker.processIter(TreeWalker.java:507)
        at com.puppycrawl.tools.checkstyle.TreeWalker.walk(TreeWalker.java:312)
        at com.puppycrawl.tools.checkstyle.TreeWalker.processFiltered(TreeWalker.java:184)
        at com.puppycrawl.tools.checkstyle.api.AbstractFileSetCheck.process(AbstractFileSetCheck.java:78)
        at com.puppycrawl.tools.checkstyle.Checker.processFile(Checker.java:316)
        at com.puppycrawl.tools.checkstyle.Checker.processFiles(Checker.java:286)
        ... 4 more
Checkstyle ends with 1 errors.

The tool crashes with a NPE for the given config and class. The problem is the array-initialization expression that creates an array with concrete values (in this case "1") and then indexes into it.

Please let me know if you want me to help with fixing this.

Attention: suppressed at circleci by - checkstyle/contribution@284acd3

@rnveach
Copy link
Member

rnveach commented Jun 30, 2017

@denizarsan Please don't report issues using snapshot version. It is unclear what commit you used or if you added any of your own custom code on top of it. Please use release versions and update your first post.

Feel free to provide us with a fix. I don't know how easy/hard this will be.

@denizarsan
Copy link
Author

@rnveach I have updated my post. I'm using the latest release (7.8.2) from https://sourceforge.net/projects/checkstyle/files/checkstyle/

@sergileon
Copy link
Contributor

I am on it

sergileon added a commit to sergileon/checkstyle that referenced this issue Aug 30, 2017
sergileon added a commit to sergileon/checkstyle that referenced this issue Aug 30, 2017
sergileon added a commit to sergileon/checkstyle that referenced this issue Aug 30, 2017
sergileon added a commit to sergileon/checkstyle that referenced this issue Aug 31, 2017
sergileon added a commit to sergileon/checkstyle that referenced this issue Aug 31, 2017
sergileon added a commit to sergileon/checkstyle that referenced this issue Aug 31, 2017
@Vladlis
Copy link
Contributor

Vladlis commented Aug 31, 2017

checkstyle/contribution@284acd3 is still to be reverted

@romani romani added the bug label Aug 31, 2017
@romani romani added this to the 8.3 milestone Aug 31, 2017
@romani
Copy link
Member

romani commented Aug 31, 2017

commit is reverted in contribution

@romani
Copy link
Member

romani commented Aug 31, 2017

I think this issue is fixed.

@romani romani closed this as completed Aug 31, 2017
@Vladlis Vladlis reopened this Sep 1, 2017
@Vladlis
Copy link
Contributor

Vladlis commented Sep 1, 2017

Caused by: com.puppycrawl.tools.checkstyle.api.CheckstyleException: Exception was thrown while processing /pipeline/source/contribution/checkstyle-tester/src/main/java/checkstyle/src/test/resources/com/puppycrawl/tools/checkstyle/checks/whitespace/nowhitespaceafter/InputNoWhitespaceAfterArrayDeclarations.java
 at com.puppycrawl.tools.checkstyle.Checker.processFiles(Checker.java:295)
 at com.puppycrawl.tools.checkstyle.Checker.process(Checker.java:213)
 at org.apache.maven.plugin.checkstyle.exec.DefaultCheckstyleExecutor.executeCheckstyle(DefaultCheckstyleExecutor.java:252)
 at org.apache.maven.plugin.checkstyle.AbstractCheckstyleReport.executeReport(AbstractCheckstyleReport.java:473)
 ... 29 more
Caused by: java.lang.NullPointerException
 at com.puppycrawl.tools.checkstyle.checks.whitespace.NoWhitespaceAfterCheck.getPositionAfter(NoWhitespaceAfterCheck.java:181)
 at com.puppycrawl.tools.checkstyle.checks.whitespace.NoWhitespaceAfterCheck.visitToken(NoWhitespaceAfterCheck.java:139)

is caught by Wercker on checkstyle/sevntu-checkstyle job after uncommenting checkstyle/contribution@284acd3, @sergileon please investigate and fix
This might be another issue

@sergileon
Copy link
Contributor

i run job on the master and not see exception

@romani
Copy link
Member

romani commented Sep 1, 2017

@Vladlis , I do not see problem on master wercker, please share link

@romani
Copy link
Member

romani commented Sep 1, 2017

I am closing issue, till smb show we exact and reproducible case

@romani romani closed this as completed Sep 1, 2017
@msimko81
Copy link

msimko81 commented May 9, 2018

Hi, I have the same problem as described in the original bug request.

If I use the following initialization block, the checkstyle plugin fails with NPE:

int group = new int[] { 1, 2, 3, 4, 9 }[RandomUtils.nextInt(0, 4)];

Splitting the code to two lines is the workaround, checkstyle is happy with this:

int[] availableGroups = new int[] { 1, 2, 3, 4, 9 };
int group = availableGroups[RandomUtils.nextInt(0, 4)];

@romani
Copy link
Member

romani commented May 9, 2018

please make sure you use checkstyle version 8.3 and above to run without exception.

@msimko81
Copy link

msimko81 commented May 9, 2018

Thanks. I have used the default plugin referenced from gradle 4.6
By specifying the checkstyle tool version 8.10, everything works now.

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

No branches or pull requests

6 participants