{"record":{"id":"ec7a5bcd4bd503c4","repo":"antlr/antlr4","slug":"labels-cannot-be-null-ec7a5b","errorCode":null,"errorMessage":"labels cannot be null","messagePattern":"labels cannot be null","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"runtime/CSharp/src/Tree/Pattern/ParseTreeMatch.cs","lineNumber":99,"sourceCode":"        /// IllegalArgumentException\n        /// if\n        /// <paramref name=\"labels\"/>\n        /// is\n        /// <see langword=\"null\"/>\n        /// </exception>\n        public ParseTreeMatch(IParseTree tree, ParseTreePattern pattern, MultiMap<string, IParseTree> labels, IParseTree mismatchedNode)\n        {\n            if (tree == null)\n            {\n                throw new ArgumentException(\"tree cannot be null\");\n            }\n            if (pattern == null)\n            {\n                throw new ArgumentException(\"pattern cannot be null\");\n            }\n            if (labels == null)\n            {\n                throw new ArgumentException(\"labels cannot be null\");\n            }\n            this.tree = tree;\n            this.pattern = pattern;\n            this.labels = labels;\n            this.mismatchedNode = mismatchedNode;\n        }\n\n        /// <summary>\n        /// Get the last node associated with a specific\n        /// <paramref name=\"label\"/>\n        /// .\n        /// <p>For example, for pattern\n        /// <c>&lt;id:ID&gt;</c>\n        /// ,\n        /// <c>get(\"id\")</c>\n        /// returns the\n        /// node matched for that\n        /// <c>ID</c>","sourceCodeStart":81,"sourceCodeEnd":117,"githubUrl":"https://github.com/antlr/antlr4/blob/7d5770395bb7b02eb56e7c62662cb1d7c08f42a3/runtime/CSharp/src/Tree/Pattern/ParseTreeMatch.cs#L81-L117","documentation":"ParseTreeMatch's constructor rejects a null labels MultiMap. The labels map (label/tag name -> matched subtrees) is the primary payload of a match result; GetAll(label), Get(label) and tree.All() downstream would all NPE if it were null. Only direct construction with a null labels map triggers this.","triggerScenarios":"new ParseTreeMatch(tree, pattern, null, mismatch); a custom MatchImpl override that forgets to forward the labels MultiMap parameter it was given.","commonSituations":"Overriding ParseTreePatternMatcher.MatchImpl and dropping the labels argument; building synthetic match results in unit tests; refactors that replaced the labels parameter with a local that is never assigned.","solutions":["Forward the labels MultiMap that MatchImpl receives: new ParseTreeMatch(tree, patternTree-matched labels map, mismatchedNode).","If constructing manually, pass new MultiMap<string, IParseTree>() instead of null when no labels are needed.","Use the standard entry point tree.Match(pattern) which always supplies a fresh labels map."],"exampleFix":"// before\nvar m = new ParseTreeMatch(tree, pattern, null, mismatch);\n\n// after\nvar labels = new MultiMap<string, IParseTree>();\nvar m = new ParseTreeMatch(tree, pattern, labels, mismatch);","handlingStrategy":"validation","validationCode":"var labels = existingLabels ?? new MultiMap<string, IParseTree>();\nvar m = new ParseTreeMatch(tree, pattern, labels, mismatch);","typeGuard":"static bool HasLabels(MultiMap<string, IParseTree> l) => l != null;","tryCatchPattern":null,"preventionTips":["Forward the labels MultiMap MatchImpl receives whenever you construct a result.","Default to a fresh MultiMap instead of null when no labels are collected."],"tags":["antlr","csharp","parse-tree-match","null-check","labels"],"backgroundTag":null,"analyzedSha":"7d5770395bb7b02eb56e7c62662cb1d7c08f42a3","analyzedAt":"2026-08-14T14:47:56.354Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}