{"record":{"id":"f80b51e8036c5f7d","repo":"dotnet/reactive","slug":"throw-new-argumentnullexception-nameof-resultselector","errorCode":null,"errorMessage":"throw new ArgumentNullException(nameof(resultSelector));","messagePattern":"throw new ArgumentNullException\\(nameof\\(resultSelector\\)\\);","errorType":"validation","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"Rx.NET/Source/src/System.Reactive/Linq/Observable.Time.cs","lineNumber":637,"sourceCode":"        /// <param name=\"resultSelector\">Selector function for results produced in the sequence.</param>\n        /// <param name=\"timeSelector\">Time selector function to control the speed of values being produced each iteration.</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=\"timeSelector\"/> 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, Func<TState, TimeSpan> timeSelector)\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            if (timeSelector == null)\n            {\n                throw new ArgumentNullException(nameof(timeSelector));\n            }\n\n            return s_impl.Generate(initialState, condition, iterate, resultSelector, timeSelector);\n        }\n\n        /// <summary>\n        /// Generates an observable sequence by running a state-driven and temporal loop producing the sequence's elements, using the specified scheduler to run timers and 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>","sourceCodeStart":619,"sourceCodeEnd":655,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/Rx.NET/Source/src/System.Reactive/Linq/Observable.Time.cs#L619-L655","documentation":"Observable.Generate (the TimeSpan timeSelector overload) throws ArgumentNullException because the resultSelector delegate was null. Rx rejects null selectors up front so the error is raised at the call site rather than mid-sequence. Supply a mapping function from state to emitted value.","triggerScenarios":"Calling Observable.Generate(initialState, condition, iterate, resultSelector: null, timeSelector) with Func<TState, TimeSpan> timeSelector — typically a forgotten lambda or a null delegate field.","commonSituations":"Dynamically composed queries where the projection lambda comes from an optional parameter or DI-resolved factory that returned null.","solutions":["Pass a non-null Func<TState, TResult> as resultSelector (e.g. state => state.ToString()).","Verify the delegate variables assigned before the call are actually initialized.","If no projection is needed, pass the identity function state => state."],"exampleFix":"// before\nFunc<int, string> sel = null;\nvar xs = Observable.Generate(0, i => i < 5, i => i + 1, sel, i => TimeSpan.FromSeconds(i));\n// after\nvar xs = Observable.Generate(0, i => i < 5, i => i + 1, i => i.ToString(), i => TimeSpan.FromSeconds(i));","handlingStrategy":"validation","validationCode":"if (resultSelector is null) throw new InvalidOperationException(\"resultSelector must be provided before calling Observable.Generate\");","typeGuard":"static bool HasSelector<TState, TResult>(Func<TState, TResult> resultSelector) => resultSelector is not null;","tryCatchPattern":"try { var xs = Observable.Generate(0, cond, iter, resultSelector, timeSel); }\ncatch (ArgumentNullException ex) when (ex.ParamName == \"resultSelector\") { /* fall back to identity projection */ }","preventionTips":["Use state => state (identity) when no projection is needed rather than null.","Check for nulls in DI-resolved or factory-produced delegates before composing queries.","Unit-test query builders with all delegates supplied."],"tags":["null-argument","rx","linq","argument-validation"],"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"}