{"record":{"id":"5db0f86c884750e7","repo":"antlr/antlr4","slug":"text-cannot-be-null","errorCode":null,"errorMessage":"text cannot be null","messagePattern":"text cannot be null","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"runtime/Java/src/org/antlr/v4/runtime/tree/pattern/TextChunk.java","lineNumber":28,"sourceCode":" * Represents a span of raw text (concrete syntax) between tags in a tree\n * pattern string.\n */\nclass TextChunk extends Chunk {\n\t/**\n\t * This is the backing field for {@link #getText}.\n\t */\n\n\tprivate final String text;\n\n\t/**\n\t * Constructs a new instance of {@link TextChunk} with the specified text.\n\t *\n\t * @param text The text of this chunk.\n\t * @exception IllegalArgumentException if {@code text} is {@code null}.\n\t */\n\tpublic TextChunk(String text) {\n\t\tif (text == null) {\n\t\t\tthrow new IllegalArgumentException(\"text cannot be null\");\n\t\t}\n\n\t\tthis.text = text;\n\t}\n\n\t/**\n\t * Gets the raw text of this chunk.\n\t *\n\t * @return The text of the chunk.\n\t */\n\n\tpublic final String getText() {\n\t\treturn text;\n\t}\n\n\t/**\n\t * {@inheritDoc}\n\t *","sourceCodeStart":10,"sourceCodeEnd":46,"githubUrl":"https://github.com/antlr/antlr4/blob/7d5770395bb7b02eb56e7c62662cb1d7c08f42a3/runtime/Java/src/org/antlr/v4/runtime/tree/pattern/TextChunk.java#L10-L46","documentation":"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.","triggerScenarios":"Calling new TextChunk(null); indirectly when ParseTreePatternBuilder splits a pattern and a literal segment is unexpectedly null.","commonSituations":"Programmatically assembling pattern chunks; passing a computed string that can be null (e.g. map lookup miss) into TextChunk.","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"],"exampleFix":"// before\nnew TextChunk(maybeNull);\n\n// after\nnew TextChunk(Objects.requireNonNull(text, \"text chunk requires literal text\"));","handlingStrategy":"validation","validationCode":"Objects.requireNonNull(text, \"text chunk must have literal text\");","typeGuard":null,"tryCatchPattern":"try { new TextChunk(text); } catch (IllegalArgumentException e) { /* reject null literal segment */ }","preventionTips":["Null-check computed strings before building chunks","Prefer compileParseTreePattern for pattern construction"],"tags":["antlr","parse-tree-pattern","null-safety","java"],"backgroundTag":null,"analyzedSha":"7d5770395bb7b02eb56e7c62662cb1d7c08f42a3","analyzedAt":"2026-08-14T14:47:56.354Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}