tui-cs/Terminal.Gui · error · ArgumentNullException

X cannot be null

Error message

X cannot be null

What it means

The X property (horizontal position) must be a non-null Pos object. Terminal.Gui's layout system is fully declarative — even 'no position' is expressed as Pos.Absolute(0), never as null. A null Pos would leave the layout engine unable to compute a coordinate.

Solutions

  1. Use Pos.Absolute(n) for fixed positions.
  2. Use Pos.Center(), Pos.Percent(n), or Pos.Left(otherView) for relative layout.
  3. If conditionally clearing, set Pos.Absolute(0) rather than null.

Example fix

// before
view.X = maybePos;
// after
view.X = maybePos ?? Pos.Absolute(0);
Defensive patterns

Strategy: validation

Validate before calling

view.X = pos ?? throw new ArgumentNullException(nameof(pos));
// or: view.X = pos ?? Pos.Absolute(0);

Type guard

static bool IsValidPos (Pos? p) => p is not null;

Prevention

When it happens

Trigger: Assigning view.X = null; or a Pos-typed variable that is null (e.g. a field never initialized, or a result of a method that returned null).

Common situations: Nullable Pos returned from a helper that returns null on edge cases; conditional initialization that leaves X unset; deserialization gap.

Related errors


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

Appendix: source

Thrown at Terminal.Gui/ViewBase/View.Layout.cs:288

    ///         resulting in the
    ///         view being laid out and redrawn as appropriate in the next iteration.
    ///     </para>
    ///     <para>
    ///         Changing this property will cause <see cref="Frame"/> to be updated.
    ///     </para>
    ///     <para>The default value is <c>Pos.Absolute (0)</c>.</para>
    /// </remarks>
    public Pos X
    {
        get => _x;
        set
        {
            if (Equals (_x, value))
            {
                return;
            }

            _x = value ?? throw new ArgumentNullException (nameof (value), @$"{nameof (X)} cannot be null");

            PosDimSet ();

            NeedsClearScreenNextIteration ();
        }
    }

    private Pos _y = Pos.Absolute (0);

    /// <summary>
    ///     Gets or sets the declarative vertical position for the view.
    /// </summary>
    /// <value>The <see cref="Pos"/> object representing the Y position.</value>
    /// <remarks>
    ///     <para>
    ///         See the View Layout Deep Dive for more information:
    ///         <see href="https://tui-cs.github.io/Terminal.Gui/docs/layout.html"/>
    ///     </para>

View on GitHub (pinned to 2e47b11478)