dotnet/machinelearning · error · ArgumentNullException

ArgumentNullException(nameof(column))

Error message

ArgumentNullException(nameof(column))

What it means

DataFrameColumnCollection.InsertItem (invoked by Insert/InsertColumn and the collection's add paths) requires a non-null DataFrameColumn; a null item is rejected immediately with ArgumentNullException(nameof(column)). This keeps the collection and its column-name index free of null entries.

Source

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

        internal void UpdateColumnNameMetadata(DataFrameColumn column, string newName)
        {
            string currentName = column.Name;
            int currentIndex = _columnNameToIndexDictionary[currentName];
            _columnNameToIndexDictionary.Remove(currentName);
            _columnNameToIndexDictionary.Add(newName, currentIndex);
            ColumnsChanged?.Invoke();
        }

        public void Insert<T>(int columnIndex, IEnumerable<T> column, string columnName)
            where T : unmanaged
        {
            DataFrameColumn newColumn = new PrimitiveDataFrameColumn<T>(columnName, column);
            Insert(columnIndex, newColumn); // calls InsertItem internally
        }

        protected override void InsertItem(int columnIndex, DataFrameColumn column)
        {
            column = column ?? throw new ArgumentNullException(nameof(column));

            if (Count == 0)
            {
                //change RowCount on inserting first row to dataframe
                RowCount = column.Length;
            }
            else if (column.Length != RowCount)
            {
                //check all columns in the dataframe have the same length (amount of rows)
                throw new ArgumentException(Strings.MismatchedColumnLengths, nameof(column));
            }

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

            column.AddOwner(this);

View on GitHub (pinned to 7b76e69cf9)

Solutions

  1. Ensure the column is constructed before insertion (e.g. new PrimitiveDataFrameColumn<T>(name, length)); check factory results for null.
  2. Guard with ArgumentNullException.ThrowIfNull or an explicit null check at the call site before inserting.
  3. Use Add/InsertColumn with a properly initialized column instead of passing through a possibly-null variable.

Example fix

// before
df.Columns.Insert(0, TryBuildColumn(name)); // TryBuildColumn can return null
// after
var col = TryBuildColumn(name) ?? throw new InvalidOperationException($"Failed to build column {name}");
df.Columns.Insert(0, col);
Defensive patterns

Strategy: type-guard

Validate before calling

if (column is null)
    throw new InvalidOperationException("Cannot insert a null column into a DataFrame.");
df.Columns.Insert(columnIndex, column);

Type guard

static bool IsValidColumn([NotNullWhen(true)] DataFrameColumn? c) => c is not null && c.Length > 0;

Try / catch

try { df.Columns.Insert(idx, column); }
catch (ArgumentNullException ex) when (ex.ParamName == "column")
{
    // column construction failed upstream; report and abort insert
}

Prevention

When it happens

Trigger: Calling df.Columns.Insert(index, null), InsertColumn with a null column, or passing a factory/lookup result that returned null into any insert/add API on DataFrameColumnCollection.

Common situations: Methods that create a column via TryGetColumn/registry lookup and forget to check for null; refactoring where a column variable became nullable; data pipelines whose column-construction helper silently returns null.

Related errors


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