Skip to content

How to resolve 'cannot initialize module TreeWalker' exception when loading checkstyle rules

1. Purpose

In this post, I will demonstrate how to resolve the following exception or error when trying to load checkstyle rules.

com.puppycrawl.tools.checkstyle.api.CheckstyleException: cannot initialize module TreeWalker - TreeWalker is not allowed as a parent of LineLength Please review 'Parent Module' section for this Check in web documentation if Check is standard.
at com.puppycrawl.tools.checkstyle.Checker.setupChild(Checker.java:482)
at com.puppycrawl.tools.checkstyle.api.AutomaticBean.configure(AutomaticBean.java:201)
at org.infernus.idea.checkstyle.service.cmd.OpCreateChecker.execute(OpCreateChecker.java:61)
at org.infernus.idea.checkstyle.service.cmd.OpCreateChecker.execute(OpCreateChecker.java:26)
at org.infernus.idea.checkstyle.service.CheckstyleActionsImpl.executeCommand(CheckstyleActionsImpl.java:130)
at org.infernus.idea.checkstyle.service.CheckstyleActionsImpl.createChecker(CheckstyleActionsImpl.java:60)
at org.infernus.idea.checkstyle.service.CheckstyleActionsImpl.createChecker(CheckstyleActionsImpl.java:51)
at org.infernus.idea.checkstyle.checker.CheckerFactoryWorker.run(CheckerFactoryWorker.java:46)
Caused by: com.puppycrawl.tools.checkstyle.api.CheckstyleException: TreeWalker is not allowed as a parent of LineLength Please review 'Parent Module' section for this Check in web documentation if Check is standard.
at com.puppycrawl.tools.checkstyle.TreeWalker.setupChild(TreeWalker.java:138)
at com.puppycrawl.tools.checkstyle.api.AutomaticBean.configure(AutomaticBean.java:201)
at com.puppycrawl.tools.checkstyle.Checker.setupChild(Checker.java:477)
... 7 more

The error screenshot is as follows:

image-20210324162026918

2. The reason and solution

2.1 Reason

There is a rule that is not supported by the latest checkstyle version. After checking, we found the problem is caused by the following checkstyle rule:

checkstyle.xml
<module name="LineLength">
<property name="max" value="200"/>
</module>

2.2 How to resolve this problem

According to pmahony893, we can remove the LineLength rule from our checkstyle file, or use the GUI to remove the check and re-add it.

Looks like this was caused by checkstyle/checkstyle#2116. It strikes me that this is going to affect a lot of people, but I’m not sure how that can be addressed from within the plugin.

A fix is to either manually edit the config file and move the LineLength element to be a direct descendent of Checker, or (using the GUI) to remove the check and re-add it.

[Ping to https://github.com/checkstyle/checkstyle/issues/7258 as well…]

2.3 The solution

We should resolve this problem by removing this rule from the file:

checkstyle.xml
<!--
<module name="LineLength">
<property name="max" value="200"/>
</module>
-->

2.4 The result

After making the changes, we found that the rules are loaded successfully by the checkstyle plugin.

image-20210324161826854

3. Summary

In this post, we demonstrated how to resolve the CheckstyleException: cannot initialize module TreeWalker error. The key takeaway is to ensure that your checkstyle rules are compatible with the latest checkstyle version. By removing or reconfiguring incompatible rules, such as LineLength, you can avoid such exceptions. Always verify your rules against the latest documentation to prevent similar issues.

Final Words + More Resources

My intention with this article was to help others who might be considering solving such a problem. So I hope that’s been the case here. If you still have any questions, don’t hesitate to ask me by email: Email me

Here are also the most important links from this article along with some further resources that will help you in this scope:

Oh, and if you found these resources useful, don’t forget to support me by starring the repo on GitHub!