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

UnnecessarySemicolonAfterTypeMemberDeclaration throws Exception on on Local Class #13411

Closed
nrmancuso opened this issue Jul 20, 2023 · 7 comments

Comments

@nrmancuso
Copy link
Member

I have read check documentation: https://checkstyle.sourceforge.io/checks/coding/unnecessarysemicolonaftertypememberdeclaration.html#UnnecessarySemicolonAfterTypeMemberDeclaration
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

➜  src javac SwitchScope.java 

➜  src cat SwitchScope.java 
public class SwitchScope {
    public static void main(String[] args) {
        switch (args.length) {
        case 0:
            final int k = 12;
            class Local {
                int j = k;
            }
        case 1:
        }
    }
}
                                                                                                           
➜  src 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="severity" value="error"/>
  <property name="fileExtensions" value="java, properties, xml"/>
  <module name="TreeWalker">
    <module name="UnnecessarySemicolonAfterTypeMemberDeclaration"/>
 </module>
</module>

➜  src java -jar checkstyle-10.12.1-all.jar -c config.xml SwitchScope.java 
Starting audit...
com.puppycrawl.tools.checkstyle.api.CheckstyleException: Exception was thrown while processing SwitchScope.java
        at com.puppycrawl.tools.checkstyle.Checker.processFiles(Checker.java:306)
        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)
Caused by: java.lang.NullPointerException: Cannot invoke "com.puppycrawl.tools.checkstyle.api.DetailAST.getType()" because "ast" is null
        at com.puppycrawl.tools.checkstyle.checks.coding.UnnecessarySemicolonAfterTypeMemberDeclarationCheck.isSemicolon(UnnecessarySemicolonAfterTypeMemberDeclarationCheck.java:236)
        at com.puppycrawl.tools.checkstyle.checks.coding.UnnecessarySemicolonAfterTypeMemberDeclarationCheck.checkTypeDefinition(UnnecessarySemicolonAfterTypeMemberDeclarationCheck.java:196)
        at com.puppycrawl.tools.checkstyle.checks.coding.UnnecessarySemicolonAfterTypeMemberDeclarationCheck.visitToken(UnnecessarySemicolonAfterTypeMemberDeclarationCheck.java:165)
        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)
        ... 5 more
Checkstyle ends with 1 errors.

Describe what you expect in detail.

I would expect this check not to throw a Null Pointer Exception on a local class declaration within a switch statement, even if this code is unusual.

@hariomch1
Copy link

Can i work on this?

@relentless-pursuit
Copy link

I have figured out where the null check can be done. Can i be assigned this?

@relentless-pursuit
Copy link

First, I tried to reproduce an issue by creating a Test.java which actually replicates the issue and a config file. And, I got a nullpointer exception as seen below.

piyush@ Development % java -jar checkstyle-10.12.2-SNAPSHOT-all.jar -c config.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:306)
	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)
Caused by: java.lang.NullPointerException: Cannot invoke "com.puppycrawl.tools.checkstyle.api.DetailAST.getType()" because "ast" is null
	at com.puppycrawl.tools.checkstyle.checks.coding.UnnecessarySemicolonAfterTypeMemberDeclarationCheck.isSemicolon(UnnecessarySemicolonAfterTypeMemberDeclarationCheck.java:236)
	at com.puppycrawl.tools.checkstyle.checks.coding.UnnecessarySemicolonAfterTypeMemberDeclarationCheck.checkTypeDefinition(UnnecessarySemicolonAfterTypeMemberDeclarationCheck.java:196)
	at com.puppycrawl.tools.checkstyle.checks.coding.UnnecessarySemicolonAfterTypeMemberDeclarationCheck.visitToken(UnnecessarySemicolonAfterTypeMemberDeclarationCheck.java:165)
	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)
	... 5 more
Checkstyle ends with 1 errors.

After, i implemented the null-check, I get the following output without any nullpointer exception.

piyush@ Development % java -jar checkstyle-10.12.2-SNAPSHOT-all.jar -c config.xml Test.java
Starting audit...
Audit done.

Is it good enough? And, can i proceed with a draft PR or a PR?

@nrmancuso
Copy link
Member Author

Anyone is welcome to send a PR, please make a comment "I am on it" in this issue to let others know you are working on it.

@relentless-pursuit
Copy link

Anyone is welcome to send a PR, please make a comment "I am on it" in this issue to let others know you are working on it.

Thank you. I am on it.

relentless-pursuit pushed a commit to relentless-pursuit/checkstyle that referenced this issue Jul 22, 2023
…cal Class (checkstyle#13411)

This commit addresses issue checkstyle#13411, where the UnnecessarySemicolonAfterTypeMemberDeclarationCheck was throwing a NullPointerException when dealing with a local class inside a switch statement.
A null check has been added to the isSemicolon method of the UnnecessarySemicolonAfterTypeMemberDeclarationCheck class to ensure that the method correctly handles null input and prevents the exception.

Resolves: checkstyle#13411 - UnnecessarySemicolonAfterTypeMemberDeclaration throws Exception on Local Class.
relentless-pursuit pushed a commit to relentless-pursuit/checkstyle that referenced this issue Jul 22, 2023
@relentless-pursuit
Copy link

@nrmancuso i have added PR. But, it is failing the CIs. Not sure why. I just made a simple change though.

relentless-pursuit pushed a commit to relentless-pursuit/checkstyle that referenced this issue Jul 23, 2023
relentless-pursuit pushed a commit to relentless-pursuit/checkstyle that referenced this issue Jul 23, 2023
relentless-pursuit pushed a commit to relentless-pursuit/checkstyle that referenced this issue Jul 23, 2023
relentless-pursuit pushed a commit to relentless-pursuit/checkstyle that referenced this issue Jul 23, 2023
relentless-pursuit pushed a commit to relentless-pursuit/checkstyle that referenced this issue Jul 23, 2023
relentless-pursuit pushed a commit to relentless-pursuit/checkstyle that referenced this issue Jul 24, 2023
relentless-pursuit pushed a commit to relentless-pursuit/checkstyle that referenced this issue Jul 24, 2023
relentless-pursuit pushed a commit to relentless-pursuit/checkstyle that referenced this issue Jul 24, 2023
relentless-pursuit pushed a commit to relentless-pursuit/checkstyle that referenced this issue Jul 24, 2023
relentless-pursuit pushed a commit to relentless-pursuit/checkstyle that referenced this issue Jul 26, 2023
relentless-pursuit pushed a commit to relentless-pursuit/checkstyle that referenced this issue Jul 26, 2023
relentless-pursuit pushed a commit to relentless-pursuit/checkstyle that referenced this issue Jul 26, 2023
relentless-pursuit pushed a commit to relentless-pursuit/checkstyle that referenced this issue Jul 26, 2023
relentless-pursuit pushed a commit to relentless-pursuit/checkstyle that referenced this issue Jul 30, 2023
relentless-pursuit pushed a commit to relentless-pursuit/checkstyle that referenced this issue Jul 30, 2023
relentless-pursuit pushed a commit to relentless-pursuit/checkstyle that referenced this issue Jul 31, 2023
relentless-pursuit pushed a commit to relentless-pursuit/checkstyle that referenced this issue Aug 1, 2023
relentless-pursuit pushed a commit to relentless-pursuit/checkstyle that referenced this issue Aug 1, 2023
@nrmancuso nrmancuso added the bug label Aug 1, 2023
@nrmancuso
Copy link
Member Author

Closed via #13486

@github-actions github-actions bot added this to the 10.12.3 milestone Aug 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
4 participants