tui-cs/Terminal.Gui · error · NullReferenceException
Target
Error message
Target
What it means
This error is thrown by PosView.ToString() when the Target property is null. PosView represents a layout position anchored to a side (Left, Right, Top, Bottom) of a target View. ToString() formats the position as 'View(Side=...,Target=...)'. If Target is null -- which should not happen in normal usage since PosView is created via Pos.Left(view), Pos.Right(view), etc. -- ToString throws a NullReferenceException rather than producing misleading output.
Source
Thrown at Terminal.Gui/ViewBase/Layout/PosView.cs:49
/// <summary>
/// Gets the View the position is anchored to.
/// </summary>
public View Target { get; }
/// <summary>
/// Gets the side of the View the position is anchored to.
/// </summary>
public Side Side { get; }
/// <inheritdoc/>
public override string ToString ()
{
var sideString = Side.ToString ();
if (Target == null)
{
throw new NullReferenceException (nameof (Target));
}
return $"View(Side={sideString},Target={Target})";
}
internal override int GetAnchor (int size) =>
Side switch
{
Side.Left => Target.Frame.X,
Side.Top => Target.Frame.Y,
Side.Right => Target.Frame.Right,
Side.Bottom => Target.Frame.Bottom,
_ => 0
};
internal override bool ReferencesOtherViews () => true;
/// <inheritdoc/>View on GitHub (pinned to 2e47b11478)
Solutions
- Ensure PosView instances are always created via Pos.Left(view)/Pos.Right(view)/Pos.Top(view)/Pos.Bottom(view) which guarantee a non-null Target.
- If deserializing, validate Target is set before use.
- Avoid disposing or nullifying views that are referenced by PosView instances.
- If this occurs during debugging, inspect the Pos hierarchy to find the partially-constructed PosView.
Example fix
// before -- a PosView with null Target throws on ToString PosView pos = new PosView(null, Side.Left); Console.WriteLine(pos.ToString()); // after -- always create PosView via the Pos factory methods Pos pos = Pos.Left(myView); Console.WriteLine(pos.ToString());
Defensive patterns
Strategy: validation
Validate before calling
// Always create PosView via factory methods which guarantee non-null Target Pos p = Pos.Left(myView); // Target is guaranteed set
Type guard
bool HasValidTarget(PosView p) => p.Target is not null;
Try / catch
try { Console.WriteLine(pos.ToString()); }
catch (NullReferenceException ex) when (ex.Message == "Target")
{ /* PosView was created without a Target -- use factory methods */ } Prevention
- Always create PosView via Pos.Left(view)/Pos.Right(view)/Pos.Top(view)/Pos.Bottom(view).
- Never construct PosView directly with a null Target.
- If deserializing, validate Target is set before use.
- Avoid disposing or nullifying views referenced by Pos instances.
When it happens
Trigger: Thrown at PosView.cs:49 when ToString() is called on a PosView instance whose Target property is null. PosView is normally created via the Pos.Left(view)/Pos.Right(view)/Pos.Top(view)/Pos.Bottom(view) factory methods, which always set Target. A null Target indicates the PosView was created via deserialization, reflection, or an internal bug.
Common situations: Debugging output (watch windows, trace logging) that calls ToString() on a Pos that was partially constructed, deserialization creating PosView without Target, or internal bugs during layout where a referenced view was disposed/nullified before ToString is called.
Related errors
- The Viewport of an AdornmentView cannot be modified.
- AdornmentView can only render to their Parent or Parent's Su
- Adornment must be of type Border
- The ShadowStyle of MarginView cannot be set
- The size of an item cannot be negative.
AI-assisted analysis of tui-cs/Terminal.Gui@2e47b11478 (2026-08-13).
Data as JSON: /api/errors/93f1aea3b684f8d1.
Report an issue: GitHub.