{"record":{"id":"2f4b9f72e4417220","repo":"antlr/antlr4","slug":"listener-2f4b9f","errorCode":null,"errorMessage":"listener","messagePattern":"listener","errorType":"exception","errorClass":"ReferenceError","httpStatus":null,"severity":"error","filePath":"runtime/Python3/src/antlr4/Parser.py","lineNumber":196,"sourceCode":"    # <em>deterministic</em>, i.e. for identical input the calls to listener\n    # methods will be the same.</p>\n    #\n    # <ul>\n    # <li>Alterations to the grammar used to generate code may change the\n    # behavior of the listener calls.</li>\n    # <li>Alterations to the command line options passed to ANTLR 4 when\n    # generating the parser may change the behavior of the listener calls.</li>\n    # <li>Changing the version of the ANTLR Tool used to generate the parser\n    # may change the behavior of the listener calls.</li>\n    # </ul>\n    #\n    # @param listener the listener to add\n    #\n    # @throws NullPointerException if {@code} listener is {@code null}\n    #\n    def addParseListener(self, listener:ParseTreeListener):\n        if listener is None:\n            raise ReferenceError(\"listener\")\n        if self._parseListeners is None:\n            self._parseListeners = []\n        self._parseListeners.append(listener)\n\n    #\n    # Remove {@code listener} from the list of parse listeners.\n    #\n    # <p>If {@code listener} is {@code null} or has not been added as a parse\n    # listener, self method does nothing.</p>\n    # @param listener the listener to remove\n    #\n    def removeParseListener(self, listener:ParseTreeListener):\n        if self._parseListeners is not None:\n            self._parseListeners.remove(listener)\n            if len(self._parseListeners)==0:\n                    self._parseListeners = None\n\n    # Remove all parse listeners.","sourceCodeStart":178,"sourceCodeEnd":214,"githubUrl":"https://github.com/antlr/antlr4/blob/7d5770395bb7b02eb56e7c62662cb1d7c08f42a3/runtime/Python3/src/antlr4/Parser.py#L178-L214","documentation":"Parser.addParseListener raises ReferenceError('listener') when called with listener=None. Parse listeners receive enterRule/exitRule/visitErrorNode callbacks during the walk performed at parse time; None is rejected because iterating listeners would otherwise crash later with a less diagnosable AttributeError.","triggerScenarios":"Passing a variable that was never assigned a listener instance; conditional listener setup ('listener = None; if debug: listener = ...' then unconditional addParseListener(listener)); refactor leftovers where the listener argument was dropped from a function signature but the call remained.","commonSituations":"Optional-listener configurations in application bootstrap; test harnesses adding a listener only in verbose mode; copy-pasted quickstart code with a placeholder None.","solutions":["Instantiate and pass a real ParseTreeListener subclass implementing enterEveryRule/exitEveryRule as needed","Guard optional listeners: if listener is not None: parser.addParseListener(listener)","Note that removeParseListener(None) is a no-op by design — only the add path validates; audit accordingly"],"exampleFix":"# before\nparser.addParseListener(listener)  # listener is None unless debug\n\n# after\nif listener is not None:\n    parser.addParseListener(listener)","handlingStrategy":"validation","validationCode":"# Python: only add a listener that exists\nif listener is not None:\n    parser.addParseListener(listener)","typeGuard":"# Python\nfrom antlr4.ParseTreeListener import ParseTreeListener\ndef is_parse_listener(obj) -> bool:\n    return isinstance(obj, ParseTreeListener)","tryCatchPattern":"try:\n    parser.addParseListener(listener)\nexcept ReferenceError as ex:\n    if str(ex) == \"listener\":\n        pass  # nothing to add; continue without listener\n    else:\n        raise","preventionTips":["Initialize listener variables to a no-op listener instance, not None","Guard conditional/debug listeners with an explicit None check","Remember removeParseListener tolerates None but addParseListener does not"],"tags":["antlr","python","parser","listeners","null-argument"],"backgroundTag":null,"analyzedSha":"7d5770395bb7b02eb56e7c62662cb1d7c08f42a3","analyzedAt":"2026-08-14T14:47:56.354Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}