{"record":{"id":"69e73336d4b9b34c","repo":"antlr/antlr4","slug":"listener-cannot-be-null","errorCode":null,"errorMessage":"listener cannot be null.","messagePattern":"listener cannot be null\\.","errorType":"exception","errorClass":"NullPointerException","httpStatus":null,"severity":"error","filePath":"runtime/Java/src/org/antlr/v4/runtime/Recognizer.java","lineNumber":215,"sourceCode":"\t\t\tif ( t.getType()==Token.EOF ) {\n\t\t\t\ts = \"<EOF>\";\n\t\t\t}\n\t\t\telse {\n\t\t\t\ts = \"<\"+t.getType()+\">\";\n\t\t\t}\n\t\t}\n\t\ts = s.replace(\"\\n\",\"\\\\n\");\n\t\ts = s.replace(\"\\r\",\"\\\\r\");\n\t\ts = s.replace(\"\\t\",\"\\\\t\");\n\t\treturn \"'\"+s+\"'\";\n\t}\n\n\t/**\n\t * @exception NullPointerException if {@code listener} is {@code null}.\n\t */\n\tpublic void addErrorListener(ANTLRErrorListener listener) {\n\t\tif (listener == null) {\n\t\t\tthrow new NullPointerException(\"listener cannot be null.\");\n\t\t}\n\n\t\t_listeners.add(listener);\n\t}\n\n\tpublic void removeErrorListener(ANTLRErrorListener listener) {\n\t\t_listeners.remove(listener);\n\t}\n\n\tpublic void removeErrorListeners() {\n\t\t_listeners.clear();\n\t}\n\n\n\tpublic List<? extends ANTLRErrorListener> getErrorListeners() {\n\t\treturn _listeners;\n\t}\n","sourceCodeStart":197,"sourceCodeEnd":233,"githubUrl":"https://github.com/antlr/antlr4/blob/7d5770395bb7b02eb56e7c62662cb1d7c08f42a3/runtime/Java/src/org/antlr/v4/runtime/Recognizer.java#L197-L233","documentation":"addErrorListener(listener) throws NullPointerException when passed null, as documented on the method. The recognizer keeps a list of error listeners and a null entry would break notification loops in reportError. It is a fail-fast argument check, not a recoverable runtime condition.","triggerScenarios":"Calling recognizer.addErrorListener(null); wiring listeners from configuration or DI where the value may be unset; copy-pasted setup code that passes a null field before initialization.","commonSituations":"Optional listener configuration (\"add listener only if configured\") written without a null branch; listener fields initialized after the recognizer is built; refactors that remove the listener creation but keep the add call.","solutions":["Remove the null listener before it reaches the call: only add when non-null","If the intent was to silence errors, call removeErrorListeners() instead of adding a null dummy","Add a no-op listener (e.g. ANTLRErrorListener with empty methods) when you need a placeholder"],"exampleFix":"// before\nparser.addErrorListener(config.getListener()); // NPE when config returns null\n\n// after\nANTLRErrorListener l = config.getListener();\nif (l != null) parser.addErrorListener(l);","handlingStrategy":"validation","validationCode":"Objects.requireNonNull(listener, \"listener\");\nif (listener != null) {\n    recognizer.addErrorListener(listener);\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Null-check optional listeners at the configuration boundary","Use removeErrorListeners() to silence, never a null listener","Annotate listener parameters with @NonNull when your toolchain supports it"],"tags":["antlr","null-check","error-listener","api-misuse"],"backgroundTag":null,"analyzedSha":"7d5770395bb7b02eb56e7c62662cb1d7c08f42a3","analyzedAt":"2026-08-14T14:47:56.354Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}