others-how to solve 'cannot initialize module TreeWalker' exception when loading checkstyle rules?

1. Purpose

In this post, I would demo how to solve the below exception or error when we try 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 latest checkstyle version, after check, we found the problem is caused by the following checkstyle rule:

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

2.2 How to sovle 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:

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

2.4 The result

After operation, we found that the rules are loaded successfully by the checkstyle plugin

image-20210324161826854

3. Summary

In this post, we demonstrated how to solve the CheckstyleException: cannot initialize module TreeWalker . Actually you should make sure that the checkstyle rules are compatible with the latest checkstyle version. There is a trick in it. Thanks for your reading. Regards.