antlr/antlr4 · error · IllegalArgumentException
text cannot be null
Error message
text cannot be null
What it means
TextChunk represents a literal-text portion of a parse-tree pattern. The constructor throws IllegalArgumentException when text is null because the chunk's whole purpose is to carry literal text used to match token text in the tree.
Source
Thrown at runtime/Java/src/org/antlr/v4/runtime/tree/pattern/TextChunk.java:28
* Represents a span of raw text (concrete syntax) between tags in a tree
* pattern string.
*/
class TextChunk extends Chunk {
/**
* This is the backing field for {@link #getText}.
*/
private final String text;
/**
* Constructs a new instance of {@link TextChunk} with the specified text.
*
* @param text The text of this chunk.
* @exception IllegalArgumentException if {@code text} is {@code null}.
*/
public TextChunk(String text) {
if (text == null) {
throw new IllegalArgumentException("text cannot be null");
}
this.text = text;
}
/**
* Gets the raw text of this chunk.
*
* @return The text of the chunk.
*/
public final String getText() {
return text;
}
/**
* {@inheritDoc}
*View on GitHub (pinned to 7d5770395b)
Solutions
- Null-check the text before constructing the chunk, or default to "" if an empty literal is intended
- Use parser.compileParseTreePattern to let ANTLR parse the pattern string correctly
Example fix
// before new TextChunk(maybeNull); // after new TextChunk(Objects.requireNonNull(text, "text chunk requires literal text"));
Defensive patterns
Strategy: validation
Validate before calling
Objects.requireNonNull(text, "text chunk must have literal text");
Try / catch
try { new TextChunk(text); } catch (IllegalArgumentException e) { /* reject null literal segment */ } Prevention
- Null-check computed strings before building chunks
- Prefer compileParseTreePattern for pattern construction
When it happens
Trigger: Calling new TextChunk(null); indirectly when ParseTreePatternBuilder splits a pattern and a literal segment is unexpectedly null.
Common situations: Programmatically assembling pattern chunks; passing a computed string that can be null (e.g. map lookup miss) into TextChunk.
Related errors
- index cannot be negative
- missing interface implementation
- Fatal error occured while evaluating the names of the gramma
- Error creating an instanceof the ANTLR tool.
- Dependency analysis failed.
AI-assisted analysis of antlr/antlr4@7d5770395b (2026-08-14).
Data as JSON: /api/errors/5db0f86c884750e7.
Report an issue: GitHub.