Unity-Technologies/UnityCsReference · error · ArgumentException

Invalid list: cannot be null

Error message

Invalid list: cannot be null

What it means

Thrown by ToggleTreeView.SearchFullTree when the rows list argument is null. The method populates the provided list with search results, so a null list means there is nowhere to store the results. This is a contract violation on the caller side, not a runtime data problem.

Source

Thrown at Editor/Mono/GUI/TreeView/ToggleTreeView.cs:79

    protected override IList<TreeViewItem> BuildRows(TreeViewItem root)
    {
        // Reuse cached list (for capacity)
        if (m_DefaultRows == null)
            m_DefaultRows = new List<TreeViewItem>(100);
        m_DefaultRows.Clear();

        if (hasSearch)
            SearchFullTree(m_DefaultRows);
        else
            AddExpandedRows(root, m_DefaultRows);
        return m_DefaultRows;
    }

    void SearchFullTree(List<TreeViewItem> rows)
    {
        if (rows == null)
            throw new ArgumentException("Invalid list: cannot be null", nameof(rows));

        var search = searchString;
        bool searchEnabledState = false;
        bool searchedEnabledState = false;
        var match = Regex.Match(search, s_Regex);
        if (match.Success)
        {
            search = match.Groups[1].Value + match.Groups[4].Value;
            searchEnabledState = true;
            searchedEnabledState = match.Groups[3].Value == "true";
        }

        var stack = new Stack<TreeViewItem>();
        stack.Push(rootItem);
        while (stack.Count > 0)
        {
            TreeViewItem current = stack.Pop();
            if (current.children != null)

View on GitHub (pinned to 225b0fbdb5)

Solutions

  1. Allocate the list before calling SearchFullTree: pass new List<TreeViewItem>() or ensure your cached field is non-null.
  2. If overriding row population, initialize m_DefaultRows in the constructor or at field declaration.
  3. Add a null-check at the call site and allocate lazily if needed.

Example fix

// before
SearchFullTree(null);

// after
if (m_DefaultRows == null) m_DefaultRows = new List<TreeViewItem>();
SearchFullTree(m_DefaultRows);
Defensive patterns

Strategy: validation

Validate before calling

if (m_DefaultRows == null) m_DefaultRows = new List<TreeViewItem>();
// now safe to call SearchFullTree

Prevention

When it happens

Trigger: Calling SearchFullTree(searchString, null) directly; passing a field that was never initialized; calling GetRows()/search with a list that a previous code path set to null.

Common situations: Custom ToggleTreeView subclass overriding row-building logic and forgetting to allocate m_DefaultRows before delegating to SearchFullTree; refactoring that moved list allocation after the search call.

Related errors


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