spectreconsole/spectre.console · error · ArgumentNullException

Value cannot be null. (Parameter 'text')

Error message

Value cannot be null. (Parameter 'text')

What it means

Thrown by the PanelHeader constructor when the text argument is null. The header text is stored and rendered; a null title is not permitted even though an empty string is allowed.

Source

Thrown at src/Spectre.Console/Widgets/PanelHeader.cs:25

{
    /// <summary>
    /// Gets the panel header text.
    /// </summary>
    public string Text { get; }

    /// <summary>
    /// Gets or sets the panel header alignment.
    /// </summary>
    public Justify? Justification { get; set; }

    /// <summary>
    /// Initializes a new instance of the <see cref="PanelHeader"/> class.
    /// </summary>
    /// <param name="text">The panel header text.</param>
    /// <param name="alignment">The panel header alignment.</param>
    public PanelHeader(string text, Justify? alignment = null)
    {
        Text = text ?? throw new ArgumentNullException(nameof(text));
        Justification = alignment;
    }

    /// <summary>
    /// Sets the panel header style.
    /// </summary>
    /// <param name="style">The panel header style.</param>
    /// <returns>The same instance so that multiple calls can be chained.</returns>
    [Obsolete("Use markup instead.")]
    [EditorBrowsable(EditorBrowsableState.Never)]
    public PanelHeader SetStyle(Style? style)
    {
        return this;
    }

    /// <summary>
    /// Sets the panel header style.
    /// </summary>

View on GitHub (pinned to 0acc92fada)

Solutions

  1. Pass a non-null string, using string.Empty if no title text is desired.
  2. Coalesce with '?? string.Empty' before constructing.
  3. Validate the title source before use.

Example fix

// before
var header = new PanelHeader(maybeNullTitle);

// after
var header = new PanelHeader(maybeNullTitle ?? string.Empty);
Defensive patterns

Strategy: validation

Validate before calling

if (text is null) throw new ArgumentNullException(nameof(text));
var header = new PanelHeader(text);

Type guard

static bool HasValidHeaderText(string? text) => text is not null;

Prevention

When it happens

Trigger: Constructing 'new PanelHeader(null)' or passing a null string variable.

Common situations: Title read from an optional config key that is absent, or a variable assigned null by default before a conditional that never sets it.

Related errors


AI-assisted analysis of spectreconsole/spectre.console@0acc92fada (2026-08-13). Data as JSON: /api/errors/a9a503c9dc234e09. Report an issue: GitHub.