{"record":{"id":"a82a10f4a8c467a3","repo":"antlr/antlr4","slug":"text-cannot-be-null-a82a10","errorCode":null,"errorMessage":"text cannot be null","messagePattern":"text cannot be null","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"runtime/CSharp/src/Tree/Pattern/TextChunk.cs","lineNumber":48,"sourceCode":"        /// <summary>\n        /// Constructs a new instance of\n        /// <see cref=\"TextChunk\"/>\n        /// with the specified text.\n        /// </summary>\n        /// <param name=\"text\">The text of this chunk.</param>\n        /// <exception>\n        /// IllegalArgumentException\n        /// if\n        /// <paramref name=\"text\"/>\n        /// is\n        /// <see langword=\"null\"/>\n        /// .\n        /// </exception>\n        public TextChunk(string text)\n        {\n            if (text == null)\n            {\n                throw new ArgumentException(\"text cannot be null\");\n            }\n            this.text = text;\n        }\n\n        /// <summary>Gets the raw text of this chunk.</summary>\n        /// <remarks>Gets the raw text of this chunk.</remarks>\n        /// <returns>The text of the chunk.</returns>\n        [NotNull]\n        public string Text\n        {\n            get\n            {\n                return text;\n            }\n        }\n\n        /// <summary>\n        /// <inheritDoc/>","sourceCodeStart":30,"sourceCodeEnd":66,"githubUrl":"https://github.com/antlr/antlr4/blob/7d5770395bb7b02eb56e7c62662cb1d7c08f42a3/runtime/CSharp/src/Tree/Pattern/TextChunk.cs#L30-L66","documentation":"TextChunk's constructor rejects a null text argument. A TextChunk is the literal-text part of a split pattern; a null text would NPE later during ToString/concatenation in the matcher, so the constructor fails fast. Note empty text is allowed (only null is rejected), unlike TagChunk which rejects both.","triggerScenarios":"Direct construction new TextChunk(null); a helper that maps optional text segments (e.g. the substring before the first tag) to TextChunk without handling the absent case.","commonSituations":"Building pattern chunks by hand instead of via ParseTreePatternMatcher.Compile; C# port differences where Java code relied on empty-string defaults; nullable string fields forwarded into chunk construction.","solutions":["Pass a non-null string — use string.Empty when there is no text.","Guard nullable inputs: text == null ? string.Empty : text.","Rely on the matcher's Compile path, which always produces non-null TextChunks from the pattern string."],"exampleFix":"// before\nchunks.Add(new TextChunk(prefix)); // prefix may be null\n\n// after\nchunks.Add(new TextChunk(prefix ?? string.Empty));","handlingStrategy":"validation","validationCode":"chunks.Add(new TextChunk(text ?? string.Empty));","typeGuard":"static bool HasText(string s) => s != null;","tryCatchPattern":null,"preventionTips":["Coalesce nullable text to string.Empty before constructing TextChunk.","Prefer the matcher's Compile path, which never produces null text chunks."],"tags":["antlr","csharp","text-chunk","null-check","pattern"],"backgroundTag":null,"analyzedSha":"7d5770395bb7b02eb56e7c62662cb1d7c08f42a3","analyzedAt":"2026-08-14T14:47:56.354Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}