{"record":{"id":"7576ae85f77516b1","repo":"antlr/antlr4","slug":"pattern-cannot-be-null-7576ae","errorCode":null,"errorMessage":"pattern cannot be null","messagePattern":"pattern cannot be null","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"runtime/Python3/src/antlr4/tree/ParseTreeMatch.py","lineNumber":37,"sourceCode":"    # 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\n    # considered to be labeled with {@code ID} and {@code expr}, respectively.</p>\n    #","sourceCodeStart":19,"sourceCodeEnd":55,"githubUrl":"https://github.com/antlr/antlr4/blob/7d5770395bb7b02eb56e7c62662cb1d7c08f42a3/runtime/Python3/src/antlr4/tree/ParseTreeMatch.py#L19-L55","documentation":"Companion check in the same constructor: the ParseTreePattern handed to ParseTreeMatch must not be None. The pattern object is normally obtained from compiler.compile(patternText, patternRuleIndex); if that result (or a hand-built pattern) is None and is passed on, this exception is raised.","triggerScenarios":"matcher.match(tree, None), or reusing a pattern variable that failed to be created (e.g. an exception during compile was swallowed and left it None).","commonSituations":"Storing compiled patterns in a dict that may not contain the key (pattern = cache.get(name) returning None); swallowing compile exceptions with a bare except and continuing with a None pattern.","solutions":["Compile the pattern once and assert it is not None before use: pattern = matcher.compile('<ID>', parser.ruleNames.index('expr')).","Use dict indexing or setdefault so a missing cached pattern raises at the lookup, not deep inside match().","Do not swallow exceptions from compile(); let them propagate or substitute a known-good pattern."],"exampleFix":"# before\npattern = pattern_cache.get(rule_name)  # None on first use\nm = matcher.match(tree, pattern)  # -> 'pattern cannot be null'\n\n# after\nif rule_name not in pattern_cache:\n    pattern_cache[rule_name] = matcher.compile(rule_pattern_text, parser.ruleNames.index(rule_name))\nm = matcher.match(tree, pattern_cache[rule_name])","handlingStrategy":"validation","validationCode":"if pattern is None:\n    pattern = matcher.compile(pattern_text, parser.ruleNames.index(rule_name))\nassert pattern is not None\nresult = matcher.match(tree, pattern)","typeGuard":"def compiled_pattern(matcher, cache, name, text, parser):\n    if name not in cache:\n        cache[name] = matcher.compile(text, parser.ruleNames.index(name))\n    return cache[name]","tryCatchPattern":null,"preventionTips":["Cache compiled patterns with explicit insertion, not dict.get that yields None","Never swallow exceptions from ParseTreePatternMatcher.compile","Compile patterns once at startup so a bad pattern fails fast, not mid-parse"],"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-15T22:17:37.221Z"}