{"record":{"id":"427523f0920bff51","repo":"dotnet/machinelearning","slug":"argumentnullexception-nameof-column","errorCode":null,"errorMessage":"ArgumentNullException(nameof(column))","messagePattern":"ArgumentNullException\\(nameof\\(column\\)\\)","errorType":"validation","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.Data.Analysis/DataFrameColumnCollection.cs","lineNumber":72,"sourceCode":"        internal void UpdateColumnNameMetadata(DataFrameColumn column, string newName)\n        {\n            string currentName = column.Name;\n            int currentIndex = _columnNameToIndexDictionary[currentName];\n            _columnNameToIndexDictionary.Remove(currentName);\n            _columnNameToIndexDictionary.Add(newName, currentIndex);\n            ColumnsChanged?.Invoke();\n        }\n\n        public void Insert<T>(int columnIndex, IEnumerable<T> column, string columnName)\n            where T : unmanaged\n        {\n            DataFrameColumn newColumn = new PrimitiveDataFrameColumn<T>(columnName, column);\n            Insert(columnIndex, newColumn); // calls InsertItem internally\n        }\n\n        protected override void InsertItem(int columnIndex, DataFrameColumn column)\n        {\n            column = column ?? throw new ArgumentNullException(nameof(column));\n\n            if (Count == 0)\n            {\n                //change RowCount on inserting first row to dataframe\n                RowCount = column.Length;\n            }\n            else if (column.Length != RowCount)\n            {\n                //check all columns in the dataframe have the same length (amount of rows)\n                throw new ArgumentException(Strings.MismatchedColumnLengths, nameof(column));\n            }\n\n            if (_columnNameToIndexDictionary.ContainsKey(column.Name))\n            {\n                throw new ArgumentException(string.Format(Strings.DuplicateColumnName, column.Name), nameof(column));\n            }\n\n            column.AddOwner(this);","sourceCodeStart":54,"sourceCodeEnd":90,"githubUrl":"https://github.com/dotnet/machinelearning/blob/7b76e69cf964daeca3f1377af6bc5543284d56c6/src/Microsoft.Data.Analysis/DataFrameColumnCollection.cs#L54-L90","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Ensure the column is constructed before insertion (e.g. new PrimitiveDataFrameColumn<T>(name, length)); check factory results for null.","Guard with ArgumentNullException.ThrowIfNull or an explicit null check at the call site before inserting.","Use Add/InsertColumn with a properly initialized column instead of passing through a possibly-null variable."],"exampleFix":"// before\ndf.Columns.Insert(0, TryBuildColumn(name)); // TryBuildColumn can return null\n// after\nvar col = TryBuildColumn(name) ?? throw new InvalidOperationException($\"Failed to build column {name}\");\ndf.Columns.Insert(0, col);","handlingStrategy":"type-guard","validationCode":"if (column is null)\n    throw new InvalidOperationException(\"Cannot insert a null column into a DataFrame.\");\ndf.Columns.Insert(columnIndex, column);","typeGuard":"static bool IsValidColumn([NotNullWhen(true)] DataFrameColumn? c) => c is not null && c.Length > 0;","tryCatchPattern":"try { df.Columns.Insert(idx, column); }\ncatch (ArgumentNullException ex) when (ex.ParamName == \"column\")\n{\n    // column construction failed upstream; report and abort insert\n}","preventionTips":["Enable nullable reference types so null columns are caught at compile time","Never let column-factory methods return null — throw instead","Null-check lookup results before inserting into df.Columns"],"tags":["dotnet","dataframe","null-argument","argument-validation"],"backgroundTag":"null-argument","analyzedSha":"7b76e69cf964daeca3f1377af6bc5543284d56c6","analyzedAt":"2026-09-11T12:35:38.930Z","contentChangedAt":"2026-09-11T12:35:38.930Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}