{"record":{"id":"3931e781c38d0232","repo":"Unity-Technologies/UnityCsReference","slug":"root-is-null","errorCode":null,"errorMessage":"root is null","messagePattern":"root is null","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"Editor/Mono/GUI/TreeView/TreeViewControl/TreeViewControl.cs","lineNumber":525,"sourceCode":"        protected IList<TIdentifier> SortItemIDsInRowOrder(IList<TIdentifier> ids)\n        {\n            return m_TreeView.SortIDsInVisiblityOrder(ids);\n        }\n\n        protected void CenterRectUsingSingleLineHeight(ref Rect rect)\n        {\n            float singleLineHeight = EditorGUIUtility.singleLineHeight;\n            if (rect.height > singleLineHeight)\n            {\n                rect.y += (rect.height - singleLineHeight) * 0.5f;\n                rect.height = singleLineHeight;\n            }\n        }\n\n        protected void AddExpandedRows(TreeViewItem<TIdentifier> root, IList<TreeViewItem<TIdentifier>> rows)\n        {\n            if (root == null)\n                throw new ArgumentNullException(\"root\", \"root is null\");\n\n            if (rows == null)\n                throw new ArgumentNullException(\"rows\", \"rows is null\");\n\n            if (root.hasChildren)\n                foreach (TreeViewItem<TIdentifier> child in root.children)\n                    GetExpandedRowsRecursive(child, rows);\n        }\n\n        void GetExpandedRowsRecursive(TreeViewItem<TIdentifier> item, IList<TreeViewItem<TIdentifier>> expandedRows)\n        {\n            if (item == null)\n                Debug.LogError(\"Found a TreeViewItem<TIdentifier> that is null. Invalid use of AddExpandedRows(): This method is only valid to call if you have built the full tree of TreeViewItems.\");\n\n            expandedRows.Add(item);\n\n            if (item.hasChildren && IsExpanded(item.id))\n                foreach (TreeViewItem<TIdentifier> child in item.children)","sourceCodeStart":507,"sourceCodeEnd":543,"githubUrl":"https://github.com/Unity-Technologies/UnityCsReference/blob/225b0fbdb57cc17d094e8056b71f8314aba56f73/Editor/Mono/GUI/TreeView/TreeViewControl/TreeViewControl.cs#L507-L543","documentation":"Thrown by AddExpandedRows when the root TreeViewItem is null. The method walks the tree from root downward collecting expanded rows; a null root means the tree was never built or BuildRoot returned null, so traversal is impossible.","triggerScenarios":"Calling AddExpandedRows before BuildRoot has run; passing rootItem when it is null because BuildRoot failed or returned null; calling during initialization before data fetch completed.","commonSituations":"Custom TreeView that overrides row-building and calls AddExpandedRows manually with an uninitialized root; race between Reload and a manual row query.","solutions":["Ensure BuildRoot has been called and returns a non-null root before invoking AddExpandedRows.","Call treeView.Reload() first to populate the root item.","Guard with: if (rootItem != null) AddExpandedRows(rootItem, rows);"],"exampleFix":"// before\nAddExpandedRows(rootItem, rows); // rootItem is null\n\n// after\ntreeView.Reload();\nif (rootItem != null)\n    AddExpandedRows(rootItem, rows);","handlingStrategy":"validation","validationCode":"treeView.Reload();\nif (rootItem != null)\n    AddExpandedRows(rootItem, rows);","typeGuard":"static bool HasValidRoot<T>(TreeViewItem<T> root) => root != null;","tryCatchPattern":null,"preventionTips":["Call Reload before any manual tree traversal.","Ensure BuildRoot returns a non-null root.","Guard AddExpandedRows with a root null check."],"tags":["unity","treeview","null-argument","buildroot","rows"],"backgroundTag":null,"analyzedSha":"225b0fbdb57cc17d094e8056b71f8314aba56f73","analyzedAt":"2026-08-13T19:07:19.849Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}