{"record":{"id":"f1d56c6fc5379c9a","repo":"Unity-Technologies/UnityCsReference","slug":"the-root-is-null","errorCode":null,"errorMessage":"The root is null","messagePattern":"The root is null","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"Editor/Mono/GUI/TreeView/TreeViewUtililty.cs","lineNumber":23,"sourceCode":"using System;\nusing System.Collections.Generic;\nusing System.Linq;\nusing Unity.Collections;\n\nnamespace UnityEditor.IMGUI.Controls\n{\n    internal static class TreeViewUtility<TIdentifier> where TIdentifier : unmanaged, IEquatable<TIdentifier>\n    {\n        internal static void SetParentAndChildrenForItems(IList<TreeViewItem<TIdentifier>> rows, TreeViewItem<TIdentifier> root)\n        {\n            SetChildParentReferences(rows, root);\n        }\n\n        // For setting depths values based on children state of the items\n        internal static void SetDepthValuesForItems(TreeViewItem<TIdentifier> root)\n        {\n            if (root == null)\n                throw new ArgumentNullException(\"root\", \"The root is null\");\n\n            Stack<TreeViewItem<TIdentifier>> stack = new Stack<TreeViewItem<TIdentifier>>();\n            stack.Push(root);\n            while (stack.Count > 0)\n            {\n                TreeViewItem<TIdentifier> current = stack.Pop();\n                if (current.children != null)\n                {\n                    foreach (var child in current.children)\n                    {\n                        if (child != null)\n                        {\n                            child.depth = current.depth + 1;\n                            stack.Push(child);\n                        }\n                    }\n                }\n            }","sourceCodeStart":5,"sourceCodeEnd":41,"githubUrl":"https://github.com/Unity-Technologies/UnityCsReference/blob/225b0fbdb57cc17d094e8056b71f8314aba56f73/Editor/Mono/GUI/TreeView/TreeViewUtililty.cs#L5-L41","documentation":"SetDepthValuesForItems walks the tree from a root to assign depth values to each item; a null root would immediately cause a NullReferenceException when pushing it onto the stack, so the method fails fast with a descriptive ArgumentNullException instead. The guard is a standard precondition: depth computation cannot proceed without a well-defined root node.","triggerScenarios":"Calling TreeViewUtility<T>.SetDepthValuesForItems(null); building a TreeViewItem hierarchy where the root failed to be constructed (e.g. BuildRoot returned null); reloading a tree view after the data source was cleared and root was set to null before the depth pass.","commonSituations":"A custom TreeView<T> whose BuildRoot returns null under an error condition; editor domain reload that nulls cached tree state; test scaffolding that constructs items without wiring a root.","solutions":["Ensure BuildRoot always returns a non-null root TreeViewItem before the tree view refreshes.","Null-check the root before calling SetDepthValuesForItems and skip/log instead of crashing.","Recreate the root (e.g. a default invisible root) when the data source is reset.","In tests, always pass a constructed root; use a guard assert in debug builds."],"exampleFix":"// before\nTreeViewUtility<int>.SetDepthValuesForItems(root);\n\n// after\nif (root == null)\n    return;\nTreeViewUtility<int>.SetDepthValuesForItems(root);","handlingStrategy":"validation","validationCode":"if (root == null) throw new ArgumentNullException(nameof(root));\nTreeViewUtility<int>.SetDepthValuesForItems(root);","typeGuard":"static bool HasRoot(TreeViewItem<int> root) => root != null;","tryCatchPattern":null,"preventionTips":["Ensure BuildRoot returns a non-null root before any Reload/refresh.","Null-check root at the boundary of tree-building utilities.","Recreate a default root when the data source is reset."],"tags":["treeview","null-argument","editor-scripting","validation"],"backgroundTag":null,"analyzedSha":"225b0fbdb57cc17d094e8056b71f8314aba56f73","analyzedAt":"2026-08-13T19:07:19.849Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}