Unity-Technologies/UnityCsReference · critical · ArgumentNullException

Invalid TreeViewState: it is null

Error message

Invalid TreeViewState: it is null

What it means

Thrown by the TreeView<TIdentifier> constructor path (via Init) when the provided TreeViewState is null. The TreeViewState carries selection, expansion, and scroll position; without it the TreeView cannot persist or render its state, so construction is rejected immediately.

Source

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

        OverriddenMethods m_OverriddenMethods;
        protected DoFoldoutCallback foldoutOverride { get; set; }

        public TreeView(TreeViewState<TIdentifier> state)
        {
            Init(state);
        }

        public TreeView(TreeViewState<TIdentifier> state, MultiColumnHeader multiColumnHeader)
        {
            m_MultiColumnHeader = multiColumnHeader;
            Init(state);
        }

        void Init(TreeViewState<TIdentifier> state)
        {
            if (state == null)
                throw new ArgumentNullException("state", "Invalid TreeViewState: it is null");

            m_TreeView = new TreeViewController<TIdentifier>(null, state);
            m_DataSource = new TreeViewControlDataSource(m_TreeView, this);
            m_GUI = new TreeViewControlGUI(m_TreeView, this);
            m_Dragging = new TreeViewControlDragging(m_TreeView, this);
            m_TreeView.Init(new Rect(), m_DataSource, m_GUI, m_Dragging);

            m_TreeView.searchChanged += SearchChanged;
            m_TreeView.selectionChangedCallback += SelectionChanged;
            m_TreeView.itemSingleClickedCallback += SingleClickedItem;
            m_TreeView.itemDoubleClickedCallback += DoubleClickedItem;
            m_TreeView.contextClickItemCallback += ContextClickedItem;
            m_TreeView.contextClickOutsideItemsCallback += ContextClicked;
            m_TreeView.expandedStateChanged += ExpandedStateChanged;
            m_TreeView.keyboardInputCallback += KeyEvent;

            m_TreeViewKeyControlID = GUIUtility.GetPermanentControlID();
        }

View on GitHub (pinned to 225b0fbdb5)

Solutions

  1. Create a new TreeViewState before constructing the TreeView: var state = new TreeViewState();
  2. When restoring, check for null and create a fresh state: state ?? new TreeViewState().
  3. Ensure the state field is assigned in OnEnable before any TreeView construction.

Example fix

// before
var tree = new TreeView<MyId>(m_State); // m_State is null

// after
m_State = m_State ?? new TreeViewState<MyId>();
var tree = new TreeView<MyId>(m_State);
Defensive patterns

Strategy: validation

Validate before calling

m_State = m_State ?? new TreeViewState();
var tree = new TreeView<int>(m_State);

Type guard

static bool IsValidState(TreeViewState s) => s != null;

Prevention

When it happens

Trigger: Constructing new TreeView(state) or new TreeView(state, multiColumnHeader) where state is null; deserializing a TreeViewState that failed to load from editor prefs and returned null.

Common situations: Creating a TreeView in an editor window without first creating/restoring the TreeViewState; passing a field that is initialized in OnEnable but the TreeView is constructed earlier; reload of a domain where the state was not serialized.

Related errors


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