{"record":{"id":"e1c3e9239c0b4e77","repo":"dotnet/reactive","slug":"value-cannot-be-null-parameter-resultselector-e1c3e9","errorCode":null,"errorMessage":"Value cannot be null. (Parameter 'resultSelector')","messagePattern":"Value cannot be null\\. \\(Parameter 'resultSelector'\\)","errorType":"validation","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"Rx.NET/Source/src/System.Reactive/Linq/Observable.Creation.cs","lineNumber":371,"sourceCode":"        /// <param name=\"iterate\">Iteration step function.</param>\n        /// <param name=\"resultSelector\">Selector function for results produced in the sequence.</param>\n        /// <returns>The generated sequence.</returns>\n        /// <exception cref=\"ArgumentNullException\"><paramref name=\"condition\"/> or <paramref name=\"iterate\"/> or <paramref name=\"resultSelector\"/> is null.</exception>\n        public static IObservable<TResult> Generate<TState, TResult>(TState initialState, Func<TState, bool> condition, Func<TState, TState> iterate, Func<TState, TResult> resultSelector)\n        {\n            if (condition == null)\n            {\n                throw new ArgumentNullException(nameof(condition));\n            }\n\n            if (iterate == null)\n            {\n                throw new ArgumentNullException(nameof(iterate));\n            }\n\n            if (resultSelector == null)\n            {\n                throw new ArgumentNullException(nameof(resultSelector));\n            }\n\n            return s_impl.Generate(initialState, condition, iterate, resultSelector);\n        }\n\n        /// <summary>\n        /// Generates an observable sequence by running a state-driven loop producing the sequence's elements, using the specified scheduler to send out observer messages.\n        /// </summary>\n        /// <typeparam name=\"TState\">The type of the state used in the generator loop.</typeparam>\n        /// <typeparam name=\"TResult\">The type of the elements in the produced sequence.</typeparam>\n        /// <param name=\"initialState\">Initial state.</param>\n        /// <param name=\"condition\">Condition to terminate generation (upon returning false).</param>\n        /// <param name=\"iterate\">Iteration step function.</param>\n        /// <param name=\"resultSelector\">Selector function for results produced in the sequence.</param>\n        /// <param name=\"scheduler\">Scheduler on which to run the generator loop.</param>\n        /// <returns>The generated sequence.</returns>\n        /// <exception cref=\"ArgumentNullException\"><paramref name=\"condition\"/> or <paramref name=\"iterate\"/> or <paramref name=\"resultSelector\"/> or <paramref name=\"scheduler\"/> is null.</exception>\n        public static IObservable<TResult> Generate<TState, TResult>(TState initialState, Func<TState, bool> condition, Func<TState, TState> iterate, Func<TState, TResult> resultSelector, IScheduler scheduler)","sourceCodeStart":353,"sourceCodeEnd":389,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/Rx.NET/Source/src/System.Reactive/Linq/Observable.Creation.cs#L353-L389","documentation":"Observable.Generate throws ArgumentNullException ('resultSelector') when the selector mapping each state value to the emitted element is null. Rx validates arguments eagerly so the failure is attributed to the caller rather than surfacing as a NullReferenceException mid-sequence.","triggerScenarios":"Calling Observable.Generate<TState, TResult>(initialState, condition, iterate, null), commonly when the projection delegate is built dynamically or read from a nullable field.","commonSituations":"Composing pipelines where the result mapping is configurable and defaults were never set; serialization/DI returning null lambdas; copy-paste refactors dropping the selector.","solutions":["Supply a non-null result selector, e.g. state => state.ToString()","If the projection is optional, use the identity function s => s","Null-check the delegate before the call"],"exampleFix":"// before\nObservable.Generate(0, c, i => i + 1, null)\n// after\nObservable.Generate(0, c, i => i + 1, i => i)","handlingStrategy":"validation","validationCode":"if (resultSelector == null) throw new ArgumentNullException(nameof(resultSelector));","typeGuard":"bool HasSelector(Func<TState,TResult> r) => r is not null;","tryCatchPattern":"try { Observable.Generate(state, cond, it, sel); } catch (ArgumentNullException ex) when (ex.ParamName == \"resultSelector\") { /* use identity */ }","preventionTips":["Use identity (s => s) when no real projection is needed","Null-check configurable selectors when loading them","Avoid returning null lambdas from factories"],"tags":["reactive","null-argument","linq"],"backgroundTag":"null-argument","analyzedSha":"94b5d5ab912789f5abe9a72138a25bbd716fe59c","analyzedAt":"2026-09-15T02:26:24.759Z","contentChangedAt":"2026-09-15T02:26:24.759Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}