{"record":{"id":"11f046fedd098d7d","repo":"dotnet/reactive","slug":"throw-new-argumentnullexception-nameof-iterate","errorCode":null,"errorMessage":"throw new ArgumentNullException(nameof(iterate));","messagePattern":"throw new ArgumentNullException\\(nameof\\(iterate\\)\\);","errorType":"validation","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"Rx.NET/Source/src/System.Reactive/Linq/Observable.Time.cs","lineNumber":632,"sourceCode":"        /// <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=\"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>","sourceCodeStart":614,"sourceCodeEnd":650,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/Rx.NET/Source/src/System.Reactive/Linq/Observable.Time.cs#L614-L650","documentation":"Observable.Generate (the TimeSpan timeSelector overload) throws ArgumentNullException because the iterate delegate parameter was null. Rx validates all delegate arguments eagerly at subscription-entry point so failures surface at call time instead of inside the running sequence. Pass a non-null state-transition function.","triggerScenarios":"Calling Observable.Generate(initialState, condition, iterate: null, resultSelector, timeSelector) where timeSelector is Func<TState, TimeSpan> — e.g. a mistyped or uninitialized Func<TState,TState> variable passed as the third delegate.","commonSituations":"Building polling/loop sequences with delays where the state-advance lambda is built conditionally or loaded from config and ends up null; refactors that swap argument order and shift a lambda into the wrong slot leaving iterate empty.","solutions":["Pass a non-null Func<TState, TState> as the iterate argument (e.g. state => state + 1).","Check argument order: Generate takes (initialState, condition, iterate, resultSelector, timeSelector[, scheduler]).","If iterate may be absent by design, supply an identity function state => state instead of null."],"exampleFix":"// before\nvar xs = Observable.Generate(0, i => i < 10, null, i => i, i => TimeSpan.FromSeconds(1));\n// after\nvar xs = Observable.Generate(0, i => i < 10, i => i + 1, i => i, i => TimeSpan.FromSeconds(1));","handlingStrategy":"validation","validationCode":"if (iterate is null) throw new InvalidOperationException(\"iterate delegate must be provided before calling Observable.Generate\");","typeGuard":"static bool HasIterate<TState>(Func<TState, TState> iterate) => iterate is not null;","tryCatchPattern":"try { var xs = Observable.Generate(0, cond, iterate, sel, timeSel); }\ncatch (ArgumentNullException ex) when (ex.ParamName == \"iterate\") { /* substitute identity or rethrow with context */ }","preventionTips":["Never pass nullable Func fields directly into Generate; assert or default them first.","Keep the Generate argument order (initialState, condition, iterate, resultSelector, timeSelector) visible when refactoring.","Prefer lambdas inline over delegate variables that can be null."],"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"}