{"record":{"id":"bf81091664a977e0","repo":"antlr/antlr4","slug":"tree-cannot-be-null-bf8109","errorCode":null,"errorMessage":"tree cannot be null","messagePattern":"tree cannot be null","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"runtime/CSharp/src/Tree/Pattern/ParseTreeMatch.cs","lineNumber":91,"sourceCode":"        /// <exception>\n        /// IllegalArgumentException\n        /// if\n        /// <paramref name=\"pattern\"/>\n        /// is\n        /// <see langword=\"null\"/>\n        /// </exception>\n        /// <exception>\n        /// 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\"/>","sourceCodeStart":73,"sourceCodeEnd":109,"githubUrl":"https://github.com/antlr/antlr4/blob/7d5770395bb7b02eb56e7c62662cb1d7c08f42a3/runtime/CSharp/src/Tree/Pattern/ParseTreeMatch.cs#L73-L109","documentation":"ParseTreeMatch's constructor rejects a null tree argument. ParseTreeMatch is the result object returned by ParseTreePattern.Match(); it must always wrap a real parse tree node. In normal use the library itself supplies a non-null tree, so hitting this means you are constructing ParseTreeMatch directly (e.g. in a custom matcher subclass) and passed null.","triggerScenarios":"Directly invoking new ParseTreeMatch(null, pattern, labels, mismatchedNode); subclassing ParseTreePatternMatcher and building the result object from a nullable tree variable without a null check.","commonSituations":"Custom pattern-matching extensions that store the matched tree in a variable defaulting to null; porting Java code where an @NotNull annotation was only advisory; test helpers that fabricate ParseTreeMatch instances.","solutions":["Pass the IParseTree instance you received in MatchImpl/tree argument — check it for null before constructing the match result.","If tree can legitimately be absent, return null (as MatchImpl does for a successful full match) instead of constructing a ParseTreeMatch with a null tree.","Use the public API tree.Pattern(pattern).Match(tree) rather than constructing ParseTreeMatch by hand."],"exampleFix":"// before\nvar m = new ParseTreeMatch(maybeTree, pattern, labels, mismatch);\n\n// after\nif (maybeTree == null) return null; // or handle absent-match case\nvar m = new ParseTreeMatch(maybeTree, pattern, labels, mismatch);","handlingStrategy":"validation","validationCode":"IParseTree maybeTree = GetTree();\nif (maybeTree == null) throw new InvalidOperationException(\"no tree to match\");\nvar m = new ParseTreeMatch(maybeTree, pattern, labels, mismatch);","typeGuard":"static bool HasTree(IParseTree t) => t != null;","tryCatchPattern":null,"preventionTips":["Do not construct ParseTreeMatch directly; use tree.Pattern(pattern).Match().","Null-check any tree variable of custom origin before building a match result."],"tags":["antlr","csharp","parse-tree-match","null-check","pattern-matching"],"backgroundTag":null,"analyzedSha":"7d5770395bb7b02eb56e7c62662cb1d7c08f42a3","analyzedAt":"2026-08-14T14:47:56.354Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}