{"record":{"id":"edc01d8f7ca88352","repo":"dotnet/reactive","slug":"thrown-when-resultselector-is-null-argumentnullexception-edc01d","errorCode":null,"errorMessage":"Thrown when resultSelector is null (ArgumentNullException, param name: resultSelector)","messagePattern":"Thrown when resultSelector is null \\(ArgumentNullException, param name: resultSelector\\)","errorType":"validation","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"Ix.NET/Source/System.Interactive/System/Linq/Operators/Generate.cs","lineNumber":28,"sourceCode":"    {\n        /// <summary>\n        /// Generates a sequence by mimicking a for loop.\n        /// </summary>\n        /// <typeparam name=\"TState\">State type.</typeparam>\n        /// <typeparam name=\"TResult\">Result sequence element type.</typeparam>\n        /// <param name=\"initialState\">Initial state of the generator loop.</param>\n        /// <param name=\"condition\">Loop condition.</param>\n        /// <param name=\"iterate\">State update function to run after every iteration of the generator loop.</param>\n        /// <param name=\"resultSelector\">Result selector to compute resulting sequence elements.</param>\n        /// <returns>Sequence obtained by running the generator loop, yielding computed elements.</returns>\n        public static IEnumerable<TResult> Generate<TState, TResult>(TState initialState, Func<TState, bool> condition, Func<TState, TState> iterate, Func<TState, TResult> resultSelector)\n        {\n            if (condition == null)\n                throw new ArgumentNullException(nameof(condition));\n            if (iterate == null)\n                throw new ArgumentNullException(nameof(iterate));\n            if (resultSelector == null)\n                throw new ArgumentNullException(nameof(resultSelector));\n\n            return GenerateCore(initialState, condition, iterate, resultSelector);\n        }\n\n        private static IEnumerable<TResult> GenerateCore<TState, TResult>(TState initialState, Func<TState, bool> condition, Func<TState, TState> iterate, Func<TState, TResult> resultSelector)\n        {\n            for (var i = initialState; condition(i); i = iterate(i))\n            {\n                yield return resultSelector(i);\n            }\n        }\n    }\n}\n","sourceCodeStart":10,"sourceCodeEnd":42,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/Ix.NET/Source/System.Interactive/System/Linq/Operators/Generate.cs#L10-L42","documentation":"Generate uses resultSelector to compute each element from the current state. A null selector cannot produce elements, so the library throws ArgumentNullException with param name 'resultSelector' before returning the sequence.","triggerScenarios":"Calling Generate(initialState, condition, iterate, null) — e.g. a projection delegate obtained from a nullable lookup or an unassigned mapping in a pipeline builder.","commonSituations":"Mapping functions supplied via options objects or DI; code-generation/reflection-based selector resolution failing; copy-paste refactors dropping the lambda argument.","solutions":["Supply a concrete result selector, e.g. state => state.ToString().","Coalesce to a default projection: resultSelector ?? (s => default(TResult)).","Fix the factory/resolver that should have produced the selector."],"exampleFix":"// before\nvar seq = GeneratorEx.Generate(0, c, it, sel); // sel is null\n// after\nvar seq = GeneratorEx.Generate(0, c, it, sel ?? (s => default(int)));","handlingStrategy":"validation","validationCode":"if (resultSelector is null) throw new ArgumentException(\"resultSelector is required\");","typeGuard":"bool HasSelector<TState, TResult>(Func<TState, TResult>? f) => f is not null;","tryCatchPattern":"try { var seq = GeneratorEx.Generate(state, condition, iterate, resultSelector); }\ncatch (ArgumentNullException ex) when (ex.ParamName == \"resultSelector\") { log.Error(\"result selector missing\"); }","preventionTips":["Keep selector lambdas inline when building generators to avoid null intermediaries.","Guard selector resolution from options/DI with a required check.","Use default projections (s => default(TResult)) over null when a mapping is genuinely absent."],"tags":["null-argument","linq","generator"],"backgroundTag":"null-argument","analyzedSha":"94b5d5ab912789f5abe9a72138a25bbd716fe59c","analyzedAt":"2026-09-15T02:26:24.759Z","contentChangedAt":"2026-09-15T02:26:24.759Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}