tui-cs/Terminal.Gui · error · ArgumentNullException

forTree

Error message

forTree

What it means

Thrown by the TreeViewTextFilter<T> constructor (ArgumentNullException, param 'forTree') when the forTree argument is null. The filter holds a reference to the TreeView it filters and calls back into it on every Text change (RefreshTreeView), so a null tree would cause NullReferenceExceptions later. The constructor rejects null up front.

Source

Thrown at Terminal.Gui/Views/TreeView/TreeViewTextFilter.cs:18

namespace Terminal.Gui.Views;

/// <summary>
///     <see cref="ITreeViewFilter{T}"/> implementation which searches the <see cref="TreeView{T}.AspectGetter"/> of
///     the model for the given <see cref="Text"/>.
/// </summary>
/// <typeparam name="T"></typeparam>
public class TreeViewTextFilter<T> : ITreeViewFilter<T> where T : class
{
    private readonly TreeView<T> _forTree;

    /// <summary>
    ///     Creates a new instance of the filter for use with <paramref name="forTree"/>. Set <see cref="Text"/> to begin
    ///     filtering.
    /// </summary>
    /// <param name="forTree">The tree view this filter applies to.</param>
    /// <exception cref="ArgumentNullException"></exception>
    public TreeViewTextFilter (TreeView<T> forTree) => _forTree = forTree ?? throw new ArgumentNullException (nameof (forTree));

    /// <summary>The case sensitivity of the search match. Defaults to <see cref="StringComparison.OrdinalIgnoreCase"/>.</summary>
    public StringComparison Comparer { get; set; } = StringComparison.OrdinalIgnoreCase;

    /// <summary>The text that will be searched for in the <see cref="TreeView{T}"/></summary>
    public string Text
    {
        get;
        set
        {
            field = value;
            RefreshTreeView ();
        }
    } = string.Empty;

    /// <summary>
    ///     Returns <see langword="true"/> if there is no <see cref="Text"/> or the text matches the
    ///     <see cref="TreeView{T}.AspectGetter"/> of the <paramref name="model"/>.

View on GitHub (pinned to 2e47b11478)

Solutions

  1. Ensure the TreeView is constructed before the filter: _tree = new TreeView<T>(); _filter = new TreeViewTextFilter<T>(_tree);
  2. If the tree may be unknown at construction, defer creating the filter until the tree is available, or set it via a method that accepts a non-null tree.
  3. In DI, register the TreeView first and inject it into the filter factory.

Example fix

// before
_filter = new TreeViewTextFilter<T>(_tree); // _tree still null -> throws 178

// after
_tree = new TreeView<T>();
_filter = new TreeViewTextFilter<T>(_tree);
Defensive patterns

Strategy: validation

Validate before calling

_tree = new TreeView<T>();
_filter = new TreeViewTextFilter<T>(_tree); // tree guaranteed non-null

Type guard

static bool TreeIsReady<T>(TreeView<T>? t) => t is not null;

Prevention

When it happens

Trigger: Constructing new TreeViewTextFilter<T>(null), or passing a tree variable that is null because it was not yet initialised (e.g. filter built before the TreeView in a field-initialiser ordering issue).

Common situations: Field/property initialisation order where the filter is created before the tree; factory/DI code that receives a null tree due to a registration miss; tests new'ing the filter without a tree.

Related errors


AI-assisted analysis of tui-cs/Terminal.Gui@2e47b11478 (2026-08-13). Data as JSON: /api/errors/dd54b12e2ff09dc7. Report an issue: GitHub.