Unity-Technologies/UnityCsReference · error · ArgumentNullException
The root is null
Error message
The root is null
What it means
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.
Source
Thrown at Editor/Mono/GUI/TreeView/TreeViewUtililty.cs:23
using System;
using System.Collections.Generic;
using System.Linq;
using Unity.Collections;
namespace UnityEditor.IMGUI.Controls
{
internal static class TreeViewUtility<TIdentifier> where TIdentifier : unmanaged, IEquatable<TIdentifier>
{
internal static void SetParentAndChildrenForItems(IList<TreeViewItem<TIdentifier>> rows, TreeViewItem<TIdentifier> root)
{
SetChildParentReferences(rows, root);
}
// For setting depths values based on children state of the items
internal static void SetDepthValuesForItems(TreeViewItem<TIdentifier> root)
{
if (root == null)
throw new ArgumentNullException("root", "The root is null");
Stack<TreeViewItem<TIdentifier>> stack = new Stack<TreeViewItem<TIdentifier>>();
stack.Push(root);
while (stack.Count > 0)
{
TreeViewItem<TIdentifier> current = stack.Pop();
if (current.children != null)
{
foreach (var child in current.children)
{
if (child != null)
{
child.depth = current.depth + 1;
stack.Push(child);
}
}
}
}View on GitHub (pinned to 225b0fbdb5)
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.
Example fix
// before
TreeViewUtility<int>.SetDepthValuesForItems(root);
// after
if (root == null)
return;
TreeViewUtility<int>.SetDepthValuesForItems(root); Defensive patterns
Strategy: validation
Validate before calling
if (root == null) throw new ArgumentNullException(nameof(root)); TreeViewUtility<int>.SetDepthValuesForItems(root);
Type guard
static bool HasRoot(TreeViewItem<int> root) => root != null;
Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- Invalid argument: {dropPosition}
- Invalid tree for finding descendants: Ensure a complete tree
- gameObjects array is null
- GameObject in gameObjects array is null
- gameObject is null
AI-assisted analysis of Unity-Technologies/UnityCsReference@225b0fbdb5 (2026-08-13).
Data as JSON: /api/errors/f1d56c6fc5379c9a.
Report an issue: GitHub.