spectreconsole/spectre.console · error · IndexOutOfRangeException

Table column index cannot be negative.

Error message

Table column index cannot be negative.

What it means

After validating the row in TableRowCollection.Update, the method checks the column index. If column < 0 it throws IndexOutOfRangeException: 'Table column index cannot be negative.' Column is indexed within the target row's renderables.

Source

Thrown at src/Spectre.Console/Widgets/Table/TableRowCollection.cs:106

        ArgumentNullException.ThrowIfNull(cellData);

        lock (_lock)
        {
            if (row < 0)
            {
                throw new IndexOutOfRangeException("Table row index cannot be negative.");
            }
            else if (row >= _list.Count)
            {
                throw new IndexOutOfRangeException("Table row index cannot exceed the number of rows in the table.");
            }

            var tableRow = _list.ElementAt(row);
            var currentRenderables = tableRow.ToList();

            if (column < 0)
            {
                throw new IndexOutOfRangeException("Table column index cannot be negative.");
            }
            else if (column >= currentRenderables.Count)
            {
                throw new IndexOutOfRangeException("Table column index cannot exceed the number of rows in the table.");
            }

            currentRenderables.RemoveAt(column);

            currentRenderables.Insert(column, cellData);

            var newTableRow = new TableRow(currentRenderables);

            _list.RemoveAt(row);

            _list.Insert(row, newTableRow);
        }
    }

View on GitHub (pinned to 0acc92fada)

Solutions

  1. Validate column >= 0 before calling Update.
  2. Bound-check against the target row's column count (table.Columns.Count) and clamp/skip.
  3. Treat column indices as zero-based and never subtract past zero.

Example fix

// before
table.Rows.Update(rowIndex, colIndex, cell);

// after
if (colIndex >= 0 && colIndex < table.Columns.Count)
    table.Rows.Update(rowIndex, colIndex, cell);
Defensive patterns

Strategy: validation

Validate before calling

if (column < 0) throw new ArgumentOutOfRangeException(nameof(column));
table.Rows.Update(row, column, cell);

Prevention

When it happens

Trigger: Calling Update with a negative column argument — typically a computed column position that underflowed to below zero.

Common situations: Off-by-one when mapping a logical column to an index; a decremented loop variable passing zero; passing an unchecked external index for the column.

Related errors


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