dotnet/machinelearning · error · ArgumentNullException

null

Error message

null

What it means

SetItem (triggered by assigning collection[index] = column or ReplaceColumn) throws ArgumentNullException when the replacement DataFrameColumn is null. The null-coalescing throw pattern uses the parameter name 'column'.

Source

Thrown at src/Microsoft.Data.Analysis/DataFrameColumnCollection.cs:105

                throw new ArgumentException(string.Format(Strings.DuplicateColumnName, column.Name), nameof(column));
            }

            column.AddOwner(this);

            RowCount = column.Length;

            _columnNameToIndexDictionary[column.Name] = columnIndex;
            for (int i = columnIndex; i < Count; i++)
            {
                _columnNameToIndexDictionary[this[i].Name]++;
            }
            base.InsertItem(columnIndex, column);
            ColumnsChanged?.Invoke();
        }

        protected override void SetItem(int columnIndex, DataFrameColumn column)
        {
            column = column ?? throw new ArgumentNullException(nameof(column));
            if (RowCount > 0 && column.Length != RowCount)
            {
                throw new ArgumentException(Strings.MismatchedColumnLengths, nameof(column));
            }
            bool existingColumn = _columnNameToIndexDictionary.TryGetValue(column.Name, out int existingColumnIndex);
            if (existingColumn && existingColumnIndex != columnIndex)
            {
                throw new ArgumentException(string.Format(Strings.DuplicateColumnName, column.Name), nameof(column));
            }

            _columnNameToIndexDictionary.Remove(this[columnIndex].Name);
            _columnNameToIndexDictionary[column.Name] = columnIndex;

            this[columnIndex].RemoveOwner(this);
            base.SetItem(columnIndex, column);

            ColumnsChanged?.Invoke();
        }

View on GitHub (pinned to 7b76e69cf9)

Solutions

  1. Pass a non-null DataFrameColumn; construct the column before assignment.
  2. Check for null before assigning and skip or throw a domain-specific error.
  3. If the intent is removal, use Remove/RemoveAt instead of assigning null.

Example fix

// before
df.Columns[0] = maybeNullColumn;
// after
if (maybeNullColumn != null)
    df.Columns[0] = maybeNullColumn;
Defensive patterns

Strategy: validation

Validate before calling

if (replacement == null)
    throw new InvalidOperationException("Replacement column must not be null");
df.Columns[index] = replacement;

Type guard

bool IsValidReplacement(DataFrameColumn c) => c != null;

Try / catch

try
{
    df.Columns[index] = column;
}
catch (ArgumentNullException ex)
{
    // column was null; construct or skip
}

Prevention

When it happens

Trigger: Executing columns[0] = null; or any API that replaces a column slot with a null reference.

Common situations: Null-propagation from a failed factory method or deserialization producing null columns, then assigning into the collection; optional column variables that turn out to be null.

Related errors


AI-assisted analysis of dotnet/machinelearning@7b76e69cf9 (2026-09-11). Data as JSON: /api/errors/5ed5102f7a9b133d. Report an issue: GitHub.