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

RequireThisCheck: Use and declare a local variable with same name as class variable cause a NPE #4777

Closed
elehuitouze opened this Issue Jul 20, 2017 · 3 comments

Comments

Projects
None yet
3 participants
@elehuitouze

elehuitouze commented Jul 20, 2017

/var/tmp $ javac LocalVariableSwitch.java 

/var/tmp $ cat LocalVariableSwitch.java

public class LocalVariableSwitch {
    private String aVariable;

    public String aMethod(int val) {
        switch (val) {
            case 0:
                String aVariable = "";
                
                if (this.aVariable != null) {
                    aVariable = this.aVariable;
                }
                
                return aVariable;
            default:
                return null;
        }
    }
}

/var/tmp $ cat config.xml

<?xml version="1.0"?>
<!DOCTYPE module PUBLIC
        "-//Puppy Crawl//DTD Check Configuration 1.3//EN"
        "http://checkstyle.sourceforge.net/dtds/configuration_1_3.dtd">
<module name="Checker">
    <module name="TreeWalker">
        <module name="RequireThisCheck" />
    </module>
</module>

/var/tmp $ java -jar checkstyle-8.0-all.jar -c config.xml LocalVariableSwitch.java

Starting audit...
com.puppycrawl.tools.checkstyle.api.CheckstyleException: Exception was thrown while processing LocalVariableSwitch.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:430)
	at com.puppycrawl.tools.checkstyle.Main.runCli(Main.java:362)
	at com.puppycrawl.tools.checkstyle.Main.main(Main.java:177)
Caused by: java.lang.NullPointerException
	at com.puppycrawl.tools.checkstyle.checks.coding.RequireThisCheck.isUserDefinedArrangementOfThis(RequireThisCheck.java:511)
	at com.puppycrawl.tools.checkstyle.checks.coding.RequireThisCheck.getClassFrameWhereViolationIsFound(RequireThisCheck.java:485)
	at com.puppycrawl.tools.checkstyle.checks.coding.RequireThisCheck.getFieldWithoutThis(RequireThisCheck.java:318)
	at com.puppycrawl.tools.checkstyle.checks.coding.RequireThisCheck.processIdent(RequireThisCheck.java:270)
	at com.puppycrawl.tools.checkstyle.checks.coding.RequireThisCheck.visitToken(RequireThisCheck.java:231)
	at com.puppycrawl.tools.checkstyle.TreeWalker.notifyVisit(TreeWalker.java:368)
	at com.puppycrawl.tools.checkstyle.TreeWalker.processIter(TreeWalker.java:505)
	at com.puppycrawl.tools.checkstyle.TreeWalker.walk(TreeWalker.java:310)
	at com.puppycrawl.tools.checkstyle.TreeWalker.processFiltered(TreeWalker.java:182)
	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.

Declare and use a local variable with the same name as a class variable in a switch cause a NPE.
It's ok (no NPE), if the local variable is declared outside the switch.

@rnveach

This comment has been minimized.

Show comment
Hide comment
@rnveach

rnveach Jul 20, 2017

Member

I can confirm exception. It started at version 6.17 .

Member

rnveach commented Jul 20, 2017

I can confirm exception. It started at version 6.17 .

@rnveach rnveach added the approved label Jul 20, 2017

@rnveach

This comment has been minimized.

Show comment
Hide comment
@rnveach

rnveach Jul 29, 2017

Member

The problem is getBlockEndToken is returning null.
blockNameIdent and blockStartToken are both set to SLIST[7x16] which is the start of the case statement. However getAllTokensOfType is looking for a right curly that ends the SLIST and it is not a right curly that does it, it is the next case group at line 11.

Member

rnveach commented Jul 29, 2017

The problem is getBlockEndToken is returning null.
blockNameIdent and blockStartToken are both set to SLIST[7x16] which is the start of the case statement. However getAllTokensOfType is looking for a right curly that ends the SLIST and it is not a right curly that does it, it is the next case group at line 11.

rnveach added a commit to rnveach/checkstyle that referenced this issue Jul 29, 2017

rnveach added a commit to rnveach/checkstyle that referenced this issue Jul 29, 2017

romani added a commit that referenced this issue Jul 31, 2017

@romani romani added the bug label Jul 31, 2017

@romani romani added this to the 8.2 milestone Jul 31, 2017

@romani

This comment has been minimized.

Show comment
Hide comment
@romani

romani Jul 31, 2017

Member

fix is merged

Member

romani commented Jul 31, 2017

fix is merged

@romani romani closed this Jul 31, 2017

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