{"record":{"id":"ad6df86a73f496a5","repo":"dotnet/machinelearning","slug":"object-reference-not-set-to-an-instance-of-an-obje","errorCode":null,"errorMessage":"Object reference not set to an instance of an object.","messagePattern":"Object reference not set to an instance of an object\\.","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.ML.CodeGenerator/CodeGenerator/CSharp/PipelineExtension.cs","lineNumber":73,"sourceCode":"\n        internal static IList<(string, string[])> GenerateTransformsAndUsings(IEnumerable<PipelineNode> nodes)\n        {\n            //var nodes = pipeline.Nodes.TakeWhile(t => t.NodeType == PipelineNodeType.Transform);\n            //var nodes = pipeline.Nodes.Where(t => t.NodeType == PipelineNodeType.Transform);\n            var results = new List<(string, string[])>();\n            foreach (var node in nodes)\n            {\n                ITransformGenerator generator = TransformGeneratorFactory.GetInstance(node);\n                results.Add((generator.GenerateTransformer(), generator.GenerateUsings()));\n            }\n\n            return results;\n        }\n\n        internal static (string, string[]) GenerateTrainerAndUsings(Pipeline pipeline)\n        {\n            if (pipeline == null)\n                throw new ArgumentNullException(nameof(pipeline));\n            try\n            {\n                var node = pipeline.Nodes.Where(t => t.NodeType == PipelineNodeType.Trainer).First();\n                ITrainerGenerator generator = TrainerGeneratorFactory.GetInstance(node);\n                var trainerString = generator.GenerateTrainer();\n                var trainerUsings = generator.GenerateUsings();\n                return (trainerString, trainerUsings);\n            }\n            catch (Exception)\n            {\n                return (string.Empty, new string[0]);\n            }\n        }\n    }\n}\n","sourceCodeStart":55,"sourceCodeEnd":89,"githubUrl":"https://github.com/dotnet/machinelearning/blob/7b76e69cf964daeca3f1377af6bc5543284d56c6/src/Microsoft.ML.CodeGenerator/CodeGenerator/CSharp/PipelineExtension.cs#L55-L89","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nvar node = pipeline.Nodes.Where(t => t.NodeType == PipelineNodeType.Trainer).First();\n// after\nvar node = pipeline.Nodes?.FirstOrDefault(t => t.NodeType == PipelineNodeType.Trainer);\nif (node == null) throw new ArgumentException(\"pipeline has no trainer node\", nameof(pipeline));","handlingStrategy":"type-guard","validationCode":"if (pipeline?.Nodes?.Any(n => n.NodeType == PipelineNodeType.Trainer) != true)\n    throw new ArgumentException(\"pipeline must contain a trainer node\", nameof(pipeline));","typeGuard":"bool HasTrainerNode(Pipeline p) => p?.Nodes?.Any(n => n.NodeType == PipelineNodeType.Trainer) == true;","tryCatchPattern":"try { var (trainer, usings) = PipelineExtension.GenerateTrainerAndUsings(pipeline); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"Sequence contains no matching\")) { /* report missing trainer node */ }","preventionTips":["Always add a trainer node to generated pipelines","Null-check pipeline.Nodes before LINQ chains","Prefer FirstOrDefault over First when emptiness is possible"],"tags":["nullref","codegen","pipeline"],"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"}