spectreconsole/spectre.console · error · ArgumentNullException

Value cannot be null. (Parameter 'title')

Error message

Value cannot be null. (Parameter 'title')

What it means

Thrown by the Rule(string) constructor when the title argument is null. Note the parameterless Rule() constructor is allowed (Title stays null and is handled in Render), but the string overload explicitly rejects null.

Source

Thrown at src/Spectre.Console/Widgets/Rule.cs:42

    public BoxBorder Border { get; set; } = BoxBorder.Square;

    internal int TitlePadding { get; set; } = 2;
    internal int TitleSpacing { get; set; } = 1;

    /// <summary>
    /// Initializes a new instance of the <see cref="Rule"/> class.
    /// </summary>
    public Rule()
    {
    }

    /// <summary>
    /// Initializes a new instance of the <see cref="Rule"/> class.
    /// </summary>
    /// <param name="title">The rule title markup text.</param>
    public Rule(string title)
    {
        Title = title ?? throw new ArgumentNullException(nameof(title));
    }

    /// <inheritdoc/>
    protected override IEnumerable<Segment> Render(RenderOptions options, int maxWidth)
    {
        var extraLength = (2 * TitlePadding) + (2 * TitleSpacing);

        if (Title == null || maxWidth <= extraLength)
        {
            return GetLineWithoutTitle(options, maxWidth);
        }

        // Get the title and make sure it fits.
        var title = GetTitleSegments(options, Title, maxWidth - extraLength);
        if (Segment.CellCount(title) > maxWidth - extraLength)
        {
            // Truncate the title
            title = Segment.TruncateWithEllipsis(title, maxWidth - extraLength);

View on GitHub (pinned to 0acc92fada)

Solutions

  1. Use the parameterless 'new Rule()' if no title is needed.
  2. Pass a non-null string or string.Empty.
  3. Coalesce the title with '?? string.Empty'.

Example fix

// before
var rule = new Rule(maybeNullTitle);

// after
var rule = maybeNullTitle == null ? new Rule() : new Rule(maybeNullTitle);
Defensive patterns

Strategy: validation

Validate before calling

var rule = title is null ? new Rule() : new Rule(title);

Prevention

When it happens

Trigger: Constructing 'new Rule(null)' or passing a null title variable to the string overload.

Common situations: Optional title from config that is absent; using the string overload where null was not intended.

Related errors


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