dotnet/machinelearning · error · ArgumentNullException
Object reference not set to an instance of an object.
Error message
Object reference not set to an instance of an object.
What it means
GenerateTrainerAndUsings locates the trainer node in a Pipeline with First() after filtering by NodeType == Trainer. If no trainer node exists, LINQ First() throws InvalidOperationException 'Sequence contains no matching element'; the NRE here surfaces when pipeline.Nodes is null (a malformed/null Nodes list), dereferenced by the Where/First chain despite the null check on pipeline itself.
Source
Thrown at src/Microsoft.ML.CodeGenerator/CodeGenerator/CSharp/PipelineExtension.cs:73
internal static IList<(string, string[])> GenerateTransformsAndUsings(IEnumerable<PipelineNode> nodes)
{
//var nodes = pipeline.Nodes.TakeWhile(t => t.NodeType == PipelineNodeType.Transform);
//var nodes = pipeline.Nodes.Where(t => t.NodeType == PipelineNodeType.Transform);
var results = new List<(string, string[])>();
foreach (var node in nodes)
{
ITransformGenerator generator = TransformGeneratorFactory.GetInstance(node);
results.Add((generator.GenerateTransformer(), generator.GenerateUsings()));
}
return results;
}
internal static (string, string[]) GenerateTrainerAndUsings(Pipeline pipeline)
{
if (pipeline == null)
throw new ArgumentNullException(nameof(pipeline));
try
{
var node = pipeline.Nodes.Where(t => t.NodeType == PipelineNodeType.Trainer).First();
ITrainerGenerator generator = TrainerGeneratorFactory.GetInstance(node);
var trainerString = generator.GenerateTrainer();
var trainerUsings = generator.GenerateUsings();
return (trainerString, trainerUsings);
}
catch (Exception)
{
return (string.Empty, new string[0]);
}
}
}
}
View on GitHub (pinned to 7b76e69cf9)
Solutions
- Ensure the Pipeline contains exactly one trainer node before generating code.
- Null-check pipeline.Nodes and the First() result; use FirstOrDefault and handle null.
- Verify the model/pipeline JSON was produced by a compatible ML.NET version and fully serialized.
Example fix
// before
var node = pipeline.Nodes.Where(t => t.NodeType == PipelineNodeType.Trainer).First();
// after
var node = pipeline.Nodes?.FirstOrDefault(t => t.NodeType == PipelineNodeType.Trainer);
if (node == null) throw new ArgumentException("pipeline has no trainer node", nameof(pipeline)); Defensive patterns
Strategy: type-guard
Validate before calling
if (pipeline?.Nodes?.Any(n => n.NodeType == PipelineNodeType.Trainer) != true)
throw new ArgumentException("pipeline must contain a trainer node", nameof(pipeline)); Type guard
bool HasTrainerNode(Pipeline p) => p?.Nodes?.Any(n => n.NodeType == PipelineNodeType.Trainer) == true;
Try / catch
try { var (trainer, usings) = PipelineExtension.GenerateTrainerAndUsings(pipeline); }
catch (InvalidOperationException ex) when (ex.Message.Contains("Sequence contains no matching")) { /* report missing trainer node */ } Prevention
- Always add a trainer node to generated pipelines
- Null-check pipeline.Nodes before LINQ chains
- Prefer FirstOrDefault over First when emptiness is possible
When it happens
Trigger: Calling code generation (GenerateTransformsAndTrainers path) with a Pipeline whose Nodes is null, or whose node list contains no Trainer node — e.g. a pipeline built solely from transforms, or deserialized model data missing the trainer.
Common situations: CodeGenerator run on an incomplete pipeline built programmatically; deserializing a stale/corrupt model summary JSON lacking the trainer node; older model files loaded with a newer generator.
Related errors
- The trainer '{trainer}' is not handled currently.
- The trainer '{node.Name}' is not handled currently.
- Value cannot be null. (Parameter 'input')
- input cannot be empty (Parameter 'input')
- The data type '{labelType}' is not handled currently.
AI-assisted analysis of dotnet/machinelearning@7b76e69cf9 (2026-09-11).
Data as JSON: /api/errors/ad6df86a73f496a5.
Report an issue: GitHub.