{"record":{"id":"93f1aea3b684f8d1","repo":"tui-cs/Terminal.Gui","slug":"target","errorCode":null,"errorMessage":"Target","messagePattern":"Target","errorType":"exception","errorClass":"NullReferenceException","httpStatus":null,"severity":"error","filePath":"Terminal.Gui/ViewBase/Layout/PosView.cs","lineNumber":49,"sourceCode":"\n    /// <summary>\n    ///     Gets the View the position is anchored to.\n    /// </summary>\n    public View Target { get; }\n\n    /// <summary>\n    ///     Gets the side of the View the position is anchored to.\n    /// </summary>\n    public Side Side { get; }\n\n    /// <inheritdoc/>\n    public override string ToString ()\n    {\n        var sideString = Side.ToString ();\n\n        if (Target == null)\n        {\n            throw new NullReferenceException (nameof (Target));\n        }\n\n        return $\"View(Side={sideString},Target={Target})\";\n    }\n\n    internal override int GetAnchor (int size) =>\n        Side switch\n        {\n            Side.Left => Target.Frame.X,\n            Side.Top => Target.Frame.Y,\n            Side.Right => Target.Frame.Right,\n            Side.Bottom => Target.Frame.Bottom,\n            _ => 0\n        };\n\n    internal override bool ReferencesOtherViews () => true;\n\n    /// <inheritdoc/>","sourceCodeStart":31,"sourceCodeEnd":67,"githubUrl":"https://github.com/tui-cs/Terminal.Gui/blob/2e47b11478db083499917f2ad27c34d30efb0df1/Terminal.Gui/ViewBase/Layout/PosView.cs#L31-L67","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before -- a PosView with null Target throws on ToString\nPosView pos = new PosView(null, Side.Left);\nConsole.WriteLine(pos.ToString());\n\n// after -- always create PosView via the Pos factory methods\nPos pos = Pos.Left(myView);\nConsole.WriteLine(pos.ToString());","handlingStrategy":"validation","validationCode":"// Always create PosView via factory methods which guarantee non-null Target\nPos p = Pos.Left(myView); // Target is guaranteed set","typeGuard":"bool HasValidTarget(PosView p) => p.Target is not null;","tryCatchPattern":"try { Console.WriteLine(pos.ToString()); }\ncatch (NullReferenceException ex) when (ex.Message == \"Target\")\n{ /* PosView was created without a Target -- use factory methods */ }","preventionTips":["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."],"tags":["layout","pos","viewbase","debugging","invariant"],"backgroundTag":null,"analyzedSha":"2e47b11478db083499917f2ad27c34d30efb0df1","analyzedAt":"2026-08-13T19:20:08.826Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}