{"record":{"id":"7ae1fee42660bb47","repo":"antlr/antlr4","slug":"tag-cannot-be-null-or-empty","errorCode":null,"errorMessage":"tag cannot be null or empty","messagePattern":"tag cannot be null or empty","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"runtime/Java/src/org/antlr/v4/runtime/tree/pattern/TagChunk.java","lineNumber":61,"sourceCode":"\tpublic TagChunk(String tag) {\n\t\tthis(null, tag);\n\t}\n\n\t/**\n\t * Construct a new instance of {@link TagChunk} using the specified label\n\t * and tag.\n\t *\n\t * @param label The label for the tag. If this is {@code null}, the\n\t * {@link TagChunk} represents an unlabeled tag.\n\t * @param tag The tag, which should be the name of a parser rule or token\n\t * type.\n\t *\n\t * @exception IllegalArgumentException if {@code tag} is {@code null} or\n\t * empty.\n\t */\n\tpublic TagChunk(String label, String tag) {\n\t\tif (tag == null || tag.isEmpty()) {\n\t\t\tthrow new IllegalArgumentException(\"tag cannot be null or empty\");\n\t\t}\n\n\t\tthis.label = label;\n\t\tthis.tag = tag;\n\t}\n\n\t/**\n\t * Get the tag for this chunk.\n\t *\n\t * @return The tag for the chunk.\n\t */\n\n\tpublic final String getTag() {\n\t\treturn tag;\n\t}\n\n\t/**\n\t * Get the label, if any, assigned to this chunk.","sourceCodeStart":43,"sourceCodeEnd":79,"githubUrl":"https://github.com/antlr/antlr4/blob/7d5770395bb7b02eb56e7c62662cb1d7c08f42a3/runtime/Java/src/org/antlr/v4/runtime/tree/pattern/TagChunk.java#L43-L79","documentation":"TagChunk is a piece of a parse-tree pattern string (e.g. the ID or expr part of '<ID:identifier>'). Its constructor rejects a null or empty tag because every tag must name a real parser rule or token type for pattern matching to work. This is an IllegalArgumentException thrown eagerly at construction time.","triggerScenarios":"Calling new TagChunk(label, tag) with tag == null or tag.isEmpty(); indirectly via ParseTreePatternBuilder when a pattern chunk is built around an empty tag substring.","commonSituations":"Building ParseTreePattern strings dynamically (e.g. from config or user input) where a tag token ends up empty, or hand-constructing chunks instead of using parser.compileParseTreePattern.","solutions":["Check the tag is non-null and non-empty before constructing the TagChunk","Prefer parser.compileParseTreePattern(pattern, ruleIndex) over hand-building TagChunk/TextChunk lists","Validate dynamically generated pattern strings (each tag between < and > must contain a rule or token name)"],"exampleFix":"// before\nnew TagChunk(label, \"\");\n\n// after\nif (tag == null || tag.isEmpty()) throw new IllegalArgumentException(\"pattern tag required\");\nnew TagChunk(label, tag);","handlingStrategy":"validation","validationCode":"boolean validTag = tag != null && !tag.isEmpty();","typeGuard":null,"tryCatchPattern":"try { new TagChunk(label, tag); } catch (IllegalArgumentException e) { /* report invalid pattern tag: \" + tag */ }","preventionTips":["Build patterns via parser.compileParseTreePattern instead of hand-constructing chunks","Validate pattern strings before use when they come from external input"],"tags":["antlr","parse-tree-pattern","validation","java"],"backgroundTag":null,"analyzedSha":"7d5770395bb7b02eb56e7c62662cb1d7c08f42a3","analyzedAt":"2026-08-14T14:47:56.354Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}