{"record":{"id":"876657decd383deb","repo":"dotnet/reactive","slug":"value-cannot-be-null-parameter-scheduler-generate","errorCode":null,"errorMessage":"Value cannot be null. (Parameter 'scheduler')","messagePattern":"Value cannot be null\\. \\(Parameter 'scheduler'\\)","errorType":"validation","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"AsyncRx.NET/System.Reactive.Async/Linq/Operators/Generate.cs","lineNumber":33,"sourceCode":"                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 Create<TResult>(observer => AsyncObserver.Generate(observer, initialState, condition, iterate, resultSelector));\n        }\n\n        public static IAsyncObservable<TResult> Generate<TState, TResult>(TState initialState, Func<TState, bool> condition, Func<TState, TState> iterate, Func<TState, TResult> resultSelector, IAsyncScheduler scheduler)\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            if (scheduler == null)\n                throw new ArgumentNullException(nameof(scheduler));\n\n            return Create<TResult>(observer => AsyncObserver.Generate(observer, initialState, condition, iterate, resultSelector, scheduler));\n        }\n\n        public static IAsyncObservable<TResult> Generate<TState, TResult>(TState initialState, Func<TState, ValueTask<bool>> condition, Func<TState, ValueTask<TState>> iterate, Func<TState, ValueTask<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 Create<TResult>(observer => AsyncObserver.Generate(observer, initialState, condition, iterate, resultSelector));\n        }\n\n        public static IAsyncObservable<TResult> Generate<TState, TResult>(TState initialState, Func<TState, ValueTask<bool>> condition, Func<TState, ValueTask<TState>> iterate, Func<TState, ValueTask<TResult>> resultSelector, IAsyncScheduler scheduler)\n        {","sourceCodeStart":15,"sourceCodeEnd":51,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/AsyncRx.NET/System.Reactive.Async/Linq/Operators/Generate.cs#L15-L51","documentation":"System.Reactive.Async's Generate operator validates all delegate arguments up front and throws ArgumentNullException when the scheduler passed to the overload Generate<TState,TResult>(initialState, condition, iterate, resultSelector, IAsyncScheduler) is null. The library does this fail-fast at subscription-creation time so a null scheduler cannot surface later deep inside async scheduling machinery. It is a caller-contract violation, not an internal bug.","triggerScenarios":"Calling System.Reactive.Async.Linq.Operators.Generate(initialState, condition, iterate, resultSelector, scheduler) with scheduler == null (line 33 of Generate.cs, the guard 'if (scheduler == null) throw new ArgumentNullException(nameof(scheduler))' in the IAsyncScheduler overload that delegates to AsyncObserver.Generate with 6 arguments).","commonSituations":"Passing a scheduler variable that was resolved from configuration/DI and came back null; refactoring code that previously called the parameterless-scheduler overload and leaving the last argument empty/nulled; a factory method returning null as the default scheduler.","solutions":["Pass a non-null IAsyncScheduler instance, e.g. AsyncScheduler.Default (or the appropriate immediate/new-thread scheduler) as the fifth argument.","If no custom scheduling is needed, call the 4-argument overload Generate(initialState, condition, iterate, resultSelector) which omits the scheduler entirely.","Null-check or require the scheduler at the call site (throw early with a clear message) before invoking Generate."],"exampleFix":"// before\nvar xs = AsyncObservable.Generate(0, i => i < 10, i => i + 1, i => i, (IAsyncScheduler)null);\n// after\nvar xs = AsyncObservable.Generate(0, i => i < 10, i => i + 1, i => i, AsyncScheduler.Default);","handlingStrategy":"validation","validationCode":"if (scheduler is null) throw new InvalidOperationException(\"Generate requires a non-null IAsyncScheduler\");\n// then call Generate(initialState, condition, iterate, resultSelector, scheduler);","typeGuard":"static bool HasScheduler(IAsyncScheduler s) => s is not null;","tryCatchPattern":"try { var xs = AsyncObservable.Generate(0, c, i, r, sched); }\ncatch (ArgumentNullException ex) when (ex.ParamName == \"scheduler\") { /* fall back to scheduler-less overload or default scheduler */ }","preventionTips":["Default to AsyncScheduler.Default instead of passing scheduler variables that may be null","Prefer the 4-argument overload when no custom scheduling is required","Enable nullable reference types so a nullable scheduler is flagged at compile time"],"tags":["argument-null","scheduler","generate","asyncrx","fail-fast"],"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"}