spectreconsole/spectre.console · error · ArgumentException

'name' cannot be null or empty.

Error message

'name' cannot be null or empty.

What it means

Thrown by Layout.GetLayout when the name argument is null or empty. A name is required to locate a child layout in the tree.

Source

Thrown at src/Spectre.Console/Widgets/Layout/Layout.cs:153

        _splitter = LayoutSplitter.Null;
        _children = [];
        _renderable = renderable ?? new LayoutPlaceholder(this);
        _ratio = 1;
        _size = null;

        Name = name;
    }

    /// <summary>
    /// Gets a child layout by it's name.
    /// </summary>
    /// <param name="name">The layout name.</param>
    /// <returns>The specified child <see cref="Layout"/>.</returns>
    public Layout GetLayout(string name)
    {
        if (string.IsNullOrEmpty(name))
        {
            throw new ArgumentException($"'{nameof(name)}' cannot be null or empty.", nameof(name));
        }

        var stack = new Stack<Layout>();
        stack.Push(this);

        while (stack.Count > 0)
        {
            var current = stack.Pop();
            if (name.Equals(current.Name, StringComparison.OrdinalIgnoreCase))
            {
                return current;
            }

            foreach (var layout in current.GetChildren())
            {
                stack.Push(layout);
            }
        }

View on GitHub (pinned to 0acc92fada)

Solutions

  1. Ensure the name argument is a non-empty string before calling GetLayout.
  2. Validate config values before use.
  3. Use string.IsNullOrWhiteSpace to guard the input.

Example fix

// before
var child = layout.GetLayout(userName); // userName may be null

// after
if (string.IsNullOrWhiteSpace(userName)) throw new ArgumentException("name required");
var child = layout.GetLayout(userName);
Defensive patterns

Strategy: validation

Validate before calling

if (string.IsNullOrEmpty(name)) throw new ArgumentException("Layout name cannot be empty");
var child = layout.GetLayout(name);

Type guard

static bool IsValidLayoutName(string name) => !string.IsNullOrEmpty(name);

Prevention

When it happens

Trigger: Calling GetLayout(null) or GetLayout(string.Empty) or GetLayout("") on a Layout instance.

Common situations: Passing a variable that was never assigned, reading a name from config that is missing, or passing an empty input from a search field.

Related errors


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