{"record":{"id":"43f1a0a110367b28","repo":"antlr/antlr4","slug":"tree-cannot-be-null-43f1a0","errorCode":null,"errorMessage":"tree cannot be null","messagePattern":"tree cannot be null","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"runtime/Python3/src/antlr4/tree/ParseTreeMatch.py","lineNumber":35,"sourceCode":"    __slots__ = ('tree', 'pattern', 'labels', 'mismatchedNode')\n    #\n    # Constructs a new instance of {@link ParseTreeMatch} from the specified\n    # parse tree and pattern.\n    #\n    # @param tree The parse tree to match against the pattern.\n    # @param pattern The parse tree pattern.\n    # @param labels A mapping from label names to collections of\n    # {@link ParseTree} objects located by the tree pattern matching process.\n    # @param mismatchedNode The first node which failed to match the tree\n    # pattern during the matching process.\n    #\n    # @exception IllegalArgumentException if {@code tree} is {@code null}\n    # @exception IllegalArgumentException if {@code pattern} is {@code null}\n    # @exception IllegalArgumentException if {@code labels} is {@code null}\n    #\n    def __init__(self, tree:ParseTree, pattern:ParseTreePattern, labels:dict, mismatchedNode:ParseTree):\n        if tree is None:\n            raise Exception(\"tree cannot be null\")\n        if pattern is None:\n            raise Exception(\"pattern cannot be null\")\n        if labels is None:\n            raise Exception(\"labels cannot be null\")\n        self.tree = tree\n        self.pattern = pattern\n        self.labels = labels\n        self.mismatchedNode = mismatchedNode\n\n    #\n    # Get the last node associated with a specific {@code label}.\n    #\n    # <p>For example, for pattern {@code <id:ID>}, {@code get(\"id\")} returns the\n    # node matched for that {@code ID}. If more than one node\n    # matched the specified label, only the last is returned. If there is\n    # no node associated with the label, this returns {@code null}.</p>\n    #\n    # <p>Pattern tags like {@code <ID>} and {@code <expr>} without labels are","sourceCodeStart":17,"sourceCodeEnd":53,"githubUrl":"https://github.com/antlr/antlr4/blob/7d5770395bb7b02eb56e7c62662cb1d7c08f42a3/runtime/Python3/src/antlr4/tree/ParseTreeMatch.py#L17-L53","documentation":"ParseTreeMatch is the result object returned by ParseTreePatternMatcher.match(); its constructor requires a non-None parse tree. 'tree cannot be null' fires when a None tree is supplied — normally internally by match(tree, pattern) when the caller passed None as the tree argument.","triggerScenarios":"Calling matcher.match(None, pattern) or ParseTreeMatch(None, pattern, labels, None); also constructing ParseTreeMatch directly with a None tree.","commonSituations":"Walking code that grabs ctx.child(i) or a labelled subtree that can be None (optional subrule not matched) and feeds it straight into pattern matching; refactoring that drops the tree variable.","solutions":["Check the tree is not None before matching: if tree is not None: m = matcher.match(tree, pattern).","For optional subrules, use a pattern that tolerates absence or check ctx.subrule_ctx is not None first.","If you construct ParseTreeMatch by hand, validate all three required arguments (tree, pattern, labels)."],"exampleFix":"# before\nm = matcher.match(ctx.expr(), pattern)  # expr() is None when the optional rule did not match\n\n# after\nexpr = ctx.expr()\nm = matcher.match(expr, pattern) if expr is not None else None","handlingStrategy":"type-guard","validationCode":"if tree is None:\n    raise ValueError('cannot pattern-match a None tree (optional subrule did not participate in the parse)')\nresult = matcher.match(tree, pattern)","typeGuard":"def matchable(tree):\n    return tree is not None and hasattr(tree, 'accept')","tryCatchPattern":null,"preventionTips":["Check optional subrule getters (ctx.expr() etc.) for None before matching","Validate tree, pattern, and labels together — the constructor rejects all three when None"],"tags":["antlr4","python","parse-tree-match","null-argument","validation"],"backgroundTag":null,"analyzedSha":"7d5770395bb7b02eb56e7c62662cb1d7c08f42a3","analyzedAt":"2026-08-14T14:47:56.354Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}