{"record":{"id":"3db8fa1a5681197c","repo":"antlr/antlr4","slug":"listener","errorCode":null,"errorMessage":"listener","messagePattern":"listener","errorType":"exception","errorClass":"NullPointerException","httpStatus":null,"severity":"error","filePath":"runtime/Java/src/org/antlr/v4/runtime/Parser.java","lineNumber":350,"sourceCode":"\t * <em>deterministic</em>, i.e. for identical input the calls to listener\n\t * methods will be the same.</p>\n\t *\n\t * <ul>\n\t * <li>Alterations to the grammar used to generate code may change the\n\t * behavior of the listener calls.</li>\n\t * <li>Alterations to the command line options passed to ANTLR 4 when\n\t * generating the parser may change the behavior of the listener calls.</li>\n\t * <li>Changing the version of the ANTLR Tool used to generate the parser\n\t * may change the behavior of the listener calls.</li>\n\t * </ul>\n\t *\n\t * @param listener the listener to add\n\t *\n\t * @throws NullPointerException if {@code} listener is {@code null}\n\t */\n\tpublic void addParseListener(ParseTreeListener listener) {\n\t\tif (listener == null) {\n\t\t\tthrow new NullPointerException(\"listener\");\n\t\t}\n\n\t\tif (_parseListeners == null) {\n\t\t\t_parseListeners = new ArrayList<ParseTreeListener>();\n\t\t}\n\n\t\tthis._parseListeners.add(listener);\n\t}\n\n\t/**\n\t * Remove {@code listener} from the list of parse listeners.\n\t *\n\t * <p>If {@code listener} is {@code null} or has not been added as a parse\n\t * listener, this method does nothing.</p>\n\t *\n\t * @see #addParseListener\n\t *\n\t * @param listener the listener to remove","sourceCodeStart":332,"sourceCodeEnd":368,"githubUrl":"https://github.com/antlr/antlr4/blob/7d5770395bb7b02eb56e7c62662cb1d7c08f42a3/runtime/Java/src/org/antlr/v4/runtime/Parser.java#L332-L368","documentation":"Parser.addParseListener() throws NullPointerException(\"listener\") when passed null, because every parse listener in the list receives enterRule/exitRule/visitTerminal callbacks and a null entry would break iteration during the parse. The check is documented in the Javadoc. Note this validates the argument, not the parser state — the listener list itself is created lazily on first add.","triggerScenarios":"parser.addParseListener(null), typically because a listener variable was never constructed or a factory/DI lookup returned null; conditional listener setup where the condition left the variable null.","commonSituations":"Optional instrumentation added only when a flag is set but added unconditionally; listener classes with constructors that can return null from a registry; test scaffolding registering mock listeners.","solutions":["Guard the registration: if (listener != null) parser.addParseListener(listener)","Fix the listener construction so it never returns null (throw early at creation instead)","When listener registration is conditional, use Optional or an explicit if-block rather than a null placeholder"],"exampleFix":"// before\nparser.addParseListener(config.isVerbose() ? verboseListener : null); // NPE when not verbose\n\n// after\nif (config.isVerbose()) parser.addParseListener(verboseListener);","handlingStrategy":"validation","validationCode":"if (listener != null) {\n  parser.addParseListener(listener);\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Guard addParseListener with a null check for optional/conditional listeners","Make listener factories throw on failure instead of returning null","Register listeners unconditionally only when construction guarantees non-null"],"tags":["antlr","parser","listener","null-check"],"backgroundTag":null,"analyzedSha":"7d5770395bb7b02eb56e7c62662cb1d7c08f42a3","analyzedAt":"2026-08-14T14:47:56.354Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}