spectreconsole/spectre.console · error · IndexOutOfRangeException
Table row index cannot exceed the number of rows in the tabl
Error message
Table row index cannot exceed the number of rows in the table.
What it means
In TableRowCollection.Update, after the negative check, if row >= _list.Count it throws IndexOutOfRangeException: 'Table row index cannot exceed the number of rows in the table.' The row must reference an existing row.
Source
Thrown at src/Spectre.Console/Widgets/Table/TableRowCollection.cs:98
/// <summary>
/// Update a table cell at the specified index.
/// </summary>
/// <param name="row">Index of cell row.</param>
/// <param name="column">index of cell column.</param>
/// <param name="cellData">The new cells details.</param>
public void Update(int row, int column, IRenderable cellData)
{
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);
View on GitHub (pinned to 0acc92fada)
Solutions
- Check row < table.Rows.Count before calling Update.
- Capture a fresh count immediately before the call rather than caching it.
- After RemoveAt/Insert, recompute any stored row indices.
Example fix
// before
table.Rows.Update(rowIndex, colIndex, cell);
// after
if (rowIndex >= 0 && rowIndex < table.Rows.Count)
table.Rows.Update(rowIndex, colIndex, cell); Defensive patterns
Strategy: validation
Validate before calling
if (row < 0 || row >= table.Rows.Count) return; // or throw table.Rows.Update(row, column, cell);
Prevention
- Check row < table.Rows.Count immediately before the call; do not cache the count.
- Treat table.Rows.Count as one-past-the-last valid index.
- After removing rows, recompute any stored indices.
When it happens
Trigger: Calling Update with a row index equal to or greater than the current number of rows (e.g. index == count, or referencing a row that was already removed).
Common situations: Stale index captured before rows were removed; assuming rows exist after dynamic modification; using table.Rows.Count (one-past-last) as a valid index; concurrent removal making the index invalid.
Related errors
- Table row index cannot be negative.
- Table column index cannot be negative.
- Table column index cannot exceed the number of rows in the t
- Value cannot be null. (Parameter 'value')
- Value cannot be null. (Parameter 'header')
AI-assisted analysis of spectreconsole/spectre.console@0acc92fada (2026-08-13).
Data as JSON: /api/errors/a7fe966dfef2e1f8.
Report an issue: GitHub.