antlr/antlr4 · error · NullPointerException
delegates
Error message
delegates
What it means
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.
Source
Thrown at runtime/Java/src/org/antlr/v4/runtime/ProxyErrorListener.java:26
import org.antlr.v4.runtime.atn.ATNConfigSet;
import org.antlr.v4.runtime.dfa.DFA;
import java.util.BitSet;
import java.util.Collection;
/**
* This implementation of {@link ANTLRErrorListener} dispatches all calls to a
* collection of delegate listeners. This reduces the effort required to support multiple
* listeners.
*
* @author Sam Harwell
*/
public class ProxyErrorListener implements ANTLRErrorListener {
private final Collection<? extends ANTLRErrorListener> delegates;
public ProxyErrorListener(Collection<? extends ANTLRErrorListener> delegates) {
if (delegates == null) {
throw new NullPointerException("delegates");
}
this.delegates = delegates;
}
@Override
public void syntaxError(Recognizer<?, ?> recognizer,
Object offendingSymbol,
int line,
int charPositionInLine,
String msg,
RecognitionException e)
{
for (ANTLRErrorListener listener : delegates) {
listener.syntaxError(recognizer, offendingSymbol, line, charPositionInLine, msg, e);
}
}
View on GitHub (pinned to 7d5770395b)
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
Example fix
// before Collection<ANTLRErrorListener> ls = maybeListeners(); // may be null new ProxyErrorListener(ls); // NPE // after new ProxyErrorListener(ls == null ? List.of() : ls);
Defensive patterns
Strategy: validation
Validate before calling
Collection<ANTLRErrorListener> safe = (delegates != null) ? delegates : Collections.emptyList(); ProxyErrorListener proxy = new ProxyErrorListener(safe);
Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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'.
Related errors
- tokenSource cannot be null
- tokens cannot be null
- listener cannot be null.
- tokenSource cannot be null
- tokens cannot be null
AI-assisted analysis of antlr/antlr4@7d5770395b (2026-08-14).
Data as JSON: /api/errors/a6a938b848cc8185.
Report an issue: GitHub.