{"record":{"id":"a6a938b848cc8185","repo":"antlr/antlr4","slug":"delegates","errorCode":null,"errorMessage":"delegates","messagePattern":"delegates","errorType":"exception","errorClass":"NullPointerException","httpStatus":null,"severity":"error","filePath":"runtime/Java/src/org/antlr/v4/runtime/ProxyErrorListener.java","lineNumber":26,"sourceCode":"import org.antlr.v4.runtime.atn.ATNConfigSet;\nimport org.antlr.v4.runtime.dfa.DFA;\n\nimport java.util.BitSet;\nimport java.util.Collection;\n\n/**\n * This implementation of {@link ANTLRErrorListener} dispatches all calls to a\n * collection of delegate listeners. This reduces the effort required to support multiple\n * listeners.\n *\n * @author Sam Harwell\n */\npublic class ProxyErrorListener implements ANTLRErrorListener {\n\tprivate final Collection<? extends ANTLRErrorListener> delegates;\n\n\tpublic ProxyErrorListener(Collection<? extends ANTLRErrorListener> delegates) {\n\t\tif (delegates == null) {\n\t\t\tthrow new NullPointerException(\"delegates\");\n\t\t}\n\n\t\tthis.delegates = delegates;\n\t}\n\n\t@Override\n\tpublic void syntaxError(Recognizer<?, ?> recognizer,\n\t\t\t\t\t\t\tObject offendingSymbol,\n\t\t\t\t\t\t\tint line,\n\t\t\t\t\t\t\tint charPositionInLine,\n\t\t\t\t\t\t\tString msg,\n\t\t\t\t\t\t\tRecognitionException e)\n\t{\n\t\tfor (ANTLRErrorListener listener : delegates) {\n\t\t\tlistener.syntaxError(recognizer, offendingSymbol, line, charPositionInLine, msg, e);\n\t\t}\n\t}\n","sourceCodeStart":8,"sourceCodeEnd":44,"githubUrl":"https://github.com/antlr/antlr4/blob/7d5770395bb7b02eb56e7c62662cb1d7c08f42a3/runtime/Java/src/org/antlr/v4/runtime/ProxyErrorListener.java#L8-L44","documentation":"ProxyErrorListener's constructor throws NullPointerException(\"delegates\") when the delegate collection is null — the proxy fans every error callback out to that collection during parsing, so a null collection cannot be iterated. An empty collection is fine (errors are simply dropped). This is a fail-fast guard for an internal composition helper most users meet indirectly.","triggerScenarios":"new ProxyErrorListener(null), usually because a varargs/collect step produced null; a helper method that builds the delegate list conditionally and returns null; DI wiring that failed to provide the listener list.","commonSituations":"Aggregating multiple error listeners (console + in-memory collector) in tooling; refactoring that moved listener-list construction; Optional-averse code paths that use null as 'no listeners'.","solutions":["Pass an empty list instead of null: new ProxyErrorListener(listeners != null ? listeners : Collections.emptyList())","Fix the producer of the delegate collection to never return null (return empty collections)","Skip constructing the proxy entirely when there are no listeners"],"exampleFix":"// before\nCollection<ANTLRErrorListener> ls = maybeListeners(); // may be null\nnew ProxyErrorListener(ls); // NPE\n\n// after\nnew ProxyErrorListener(ls == null ? List.of() : ls);","handlingStrategy":"validation","validationCode":"Collection<ANTLRErrorListener> safe = (delegates != null) ? delegates : Collections.emptyList();\nProxyErrorListener proxy = new ProxyErrorListener(safe);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Never pass null delegate collections — use an empty collection","Have collection-producing helpers return empty collections instead of null","Construct the proxy only when at least one delegate exists"],"tags":["antlr","error-listener","null-check","constructor"],"backgroundTag":null,"analyzedSha":"7d5770395bb7b02eb56e7c62662cb1d7c08f42a3","analyzedAt":"2026-08-14T14:47:56.354Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}