Unity-Technologies/UnityCsReference · error · ArgumentNullException

root is null

Error message

root is null

What it means

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.

Source

Thrown at Editor/Mono/GUI/TreeView/TreeViewControl/TreeViewControl.cs:525

        protected IList<TIdentifier> SortItemIDsInRowOrder(IList<TIdentifier> ids)
        {
            return m_TreeView.SortIDsInVisiblityOrder(ids);
        }

        protected void CenterRectUsingSingleLineHeight(ref Rect rect)
        {
            float singleLineHeight = EditorGUIUtility.singleLineHeight;
            if (rect.height > singleLineHeight)
            {
                rect.y += (rect.height - singleLineHeight) * 0.5f;
                rect.height = singleLineHeight;
            }
        }

        protected void AddExpandedRows(TreeViewItem<TIdentifier> root, IList<TreeViewItem<TIdentifier>> rows)
        {
            if (root == null)
                throw new ArgumentNullException("root", "root is null");

            if (rows == null)
                throw new ArgumentNullException("rows", "rows is null");

            if (root.hasChildren)
                foreach (TreeViewItem<TIdentifier> child in root.children)
                    GetExpandedRowsRecursive(child, rows);
        }

        void GetExpandedRowsRecursive(TreeViewItem<TIdentifier> item, IList<TreeViewItem<TIdentifier>> expandedRows)
        {
            if (item == null)
                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.");

            expandedRows.Add(item);

            if (item.hasChildren && IsExpanded(item.id))
                foreach (TreeViewItem<TIdentifier> child in item.children)

View on GitHub (pinned to 225b0fbdb5)

Solutions

  1. Ensure BuildRoot has been called and returns a non-null root before invoking AddExpandedRows.
  2. Call treeView.Reload() first to populate the root item.
  3. Guard with: if (rootItem != null) AddExpandedRows(rootItem, rows);

Example fix

// before
AddExpandedRows(rootItem, rows); // rootItem is null

// after
treeView.Reload();
if (rootItem != null)
    AddExpandedRows(rootItem, rows);
Defensive patterns

Strategy: validation

Validate before calling

treeView.Reload();
if (rootItem != null)
    AddExpandedRows(rootItem, rows);

Type guard

static bool HasValidRoot<T>(TreeViewItem<T> root) => root != null;

Prevention

When it happens

Trigger: 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.

Common situations: Custom TreeView that overrides row-building and calls AddExpandedRows manually with an uninitialized root; race between Reload and a manual row query.

Related errors


AI-assisted analysis of Unity-Technologies/UnityCsReference@225b0fbdb5 (2026-08-13). Data as JSON: /api/errors/3931e781c38d0232. Report an issue: GitHub.