dotnet/machinelearning · critical · ArgumentNullException
Training data cannot be null
Error message
Training data cannot be null
What it means
Thrown by UserInputValidationUtil.ValidateTrainData when the training IDataView passed to an AutoML experiment is null. The library requires a non-null training dataset to run any experiment and reports the failure via ArgumentNullException naming the trainData parameter.
Source
Thrown at src/Microsoft.ML.AutoML/Utils/UserInputValidationUtil.cs:73
if (numberOfCVFolds <= 1)
{
throw new ArgumentException($"{nameof(numberOfCVFolds)} must be at least 2", nameof(numberOfCVFolds));
}
}
public static void ValidateSamplingKey(string samplingKeyColumnName, string groupIdColumnName, TaskKind task)
{
if (task == TaskKind.Ranking && samplingKeyColumnName != null && samplingKeyColumnName != groupIdColumnName)
{
throw new ArgumentException($"If provided, {nameof(samplingKeyColumnName)} must be the same as {nameof(groupIdColumnName)} for Ranking Experiments", samplingKeyColumnName);
}
}
private static void ValidateTrainData(IDataView trainData, ColumnInformation columnInformation)
{
if (trainData == null)
{
throw new ArgumentNullException(nameof(trainData), "Training data cannot be null");
}
if (DatasetDimensionsUtil.IsDataViewEmpty(trainData))
{
throw new ArgumentException("Training data has 0 rows", nameof(trainData));
}
foreach (var column in trainData.Schema)
{
if (column.Name == DefaultColumnNames.Features && column.Type.GetItemType() != NumberDataViewType.Single)
{
throw new ArgumentException($"{DefaultColumnNames.Features} column must be of data type {NumberDataViewType.Single}", nameof(trainData));
}
if ((column.Name != columnInformation.LabelColumnName &&
column.Name != columnInformation.UserIdColumnName &&
column.Name != columnInformation.ItemIdColumnName &&
column.Name != columnInformation.GroupIdColumnName)View on GitHub (pinned to 7b76e69cf9)
Solutions
- Load a valid IDataView before calling Execute (e.g. mlContext.Data.LoadFromTextFile<T>(path))
- Add a null check on the training data before invoking the experiment
- Fix the upstream data-loading code that silently returned null
Example fix
// before
IDataView trainData = LoadData(); // may return null
var result = experiment.Execute(trainData, labelColumnName, "A");
// after
IDataView trainData = LoadData() ?? mlContext.Data.LoadFromTextFile<ModelInput>(dataPath, hasHeader: true, separatorChar: ',');
if (trainData == null) throw new InvalidOperationException("No training data loaded");
var result = experiment.Execute(trainData, labelColumnName, "A"); Defensive patterns
Strategy: type-guard
Validate before calling
if (trainData is null) throw new InvalidOperationException("trainData must be loaded before Execute"); Type guard
static bool HasTrainingData(IDataView d) => d is not null && d.GetRowCursor(d.Schema).MoveNext();
Try / catch
try { var r = experiment.Execute(trainData, label, "A"); }
catch (ArgumentNullException ex) when (ex.ParamName == "trainData") { /* load default dataset and retry once */ } Prevention
- Never let data-loading helpers return null; throw early inside the loader
- Load data immediately before Execute in the same method
- Use nullable reference types (IDataView?) so the compiler flags null flows
When it happens
Trigger: Calling experiment.Execute(null, ...) or ValidateExperimentExecuteArgs with trainData = null, typically when a data-loading step returned null.
Common situations: LoadFromTextFile or a custom loader returning null after a failed read; a lazily-initialized dataset variable never assigned; refactoring that removed data loading but left the Execute call in place.
Related errors
- Training data has 0 rows
- Provided label column cannot be null
- Provided path cannot be null
- If provided, {nameof(samplingKeyColumnName)} must be the sam
- {DefaultColumnNames.Features} column must be of data type {N
AI-assisted analysis of dotnet/machinelearning@7b76e69cf9 (2026-09-11).
Data as JSON: /api/errors/2eadda3f938aa13e.
Report an issue: GitHub.