{"record":{"id":"57e3a02665437038","repo":"antlr/antlr4","slug":"patterntree-cannot-be-null","errorCode":null,"errorMessage":"patternTree cannot be null","messagePattern":"patternTree cannot be null","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"runtime/Java/src/org/antlr/v4/runtime/tree/pattern/ParseTreePatternMatcher.java","lineNumber":265,"sourceCode":"\t * Recursively walk {@code tree} against {@code patternTree}, filling\n\t * {@code match.}{@link ParseTreeMatch#labels labels}.\n\t *\n\t * @return the first node encountered in {@code tree} which does not match\n\t * a corresponding node in {@code patternTree}, or {@code null} if the match\n\t * was successful. The specific node returned depends on the matching\n\t * algorithm used by the implementation, and may be overridden.\n\t */\n\n\tprotected ParseTree matchImpl(ParseTree tree,\n\t\t\t\t\t\t\t\t  ParseTree patternTree,\n\t\t\t\t\t\t\t\t  MultiMap<String, ParseTree> labels)\n\t{\n\t\tif (tree == null) {\n\t\t\tthrow new IllegalArgumentException(\"tree cannot be null\");\n\t\t}\n\n\t\tif (patternTree == null) {\n\t\t\tthrow new IllegalArgumentException(\"patternTree cannot be null\");\n\t\t}\n\n\t\t// x and <ID>, x and y, or x and x; or could be mismatched types\n\t\tif ( tree instanceof TerminalNode && patternTree instanceof TerminalNode ) {\n\t\t\tTerminalNode t1 = (TerminalNode)tree;\n\t\t\tTerminalNode t2 = (TerminalNode)patternTree;\n\t\t\tParseTree mismatchedNode = null;\n\t\t\t// both are tokens and they have same type\n\t\t\tif ( t1.getSymbol().getType() == t2.getSymbol().getType() ) {\n\t\t\t\tif ( t2.getSymbol() instanceof TokenTagToken ) { // x and <ID>\n\t\t\t\t\tTokenTagToken tokenTagToken = (TokenTagToken)t2.getSymbol();\n\t\t\t\t\t// track label->list-of-nodes for both token name and label (if any)\n\t\t\t\t\tlabels.map(tokenTagToken.getTokenName(), tree);\n\t\t\t\t\tif ( tokenTagToken.getLabel()!=null ) {\n\t\t\t\t\t\tlabels.map(tokenTagToken.getLabel(), tree);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\telse if ( t1.getText().equals(t2.getText()) ) {","sourceCodeStart":247,"sourceCodeEnd":283,"githubUrl":"https://github.com/antlr/antlr4/blob/7d5770395bb7b02eb56e7c62662cb1d7c08f42a3/runtime/Java/src/org/antlr/v4/runtime/tree/pattern/ParseTreePatternMatcher.java#L247-L283","documentation":"Error \"patternTree cannot be null\" thrown in antlr/antlr4.","triggerScenarios":"ParseTreePatternMatcher.matchImpl() invoked with a null patternTree.","commonSituations":"Compile patterns via compile() before matching; do not pass a null pattern tree into matchImpl.","solutions":["Pass a non-null pattern tree when constructing the pattern-matching internals.","Use ParseTreePatternMatcher.compile(pattern, ruleIndex) which builds the pattern tree for you, instead of hand-constructing it."],"exampleFix":"ParseTreePattern p = matcher.compile(\"<ID> = <expr>;\", MyParser.RULE_assign);","handlingStrategy":null,"validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"7d5770395bb7b02eb56e7c62662cb1d7c08f42a3","analyzedAt":"2026-08-14T14:47:56.354Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}