dotnet/reactive · error · ArgumentNullException
Value cannot be null. (Parameter 'iterate')
Error message
Value cannot be null. (Parameter 'iterate')
What it means
The scheduler-based Generate overload throws ArgumentNullException with parameter name 'iterate' when the state-advance function is null. The full .NET message is 'Value cannot be null. (Parameter 'iterate')'.
Solutions
- Provide a non-null step function like x => x + 1 for the third argument
- Check argument positions against the (initialState, condition, iterate, resultSelector, scheduler) signature
- Fail fast at construction of the delegates: if (iterate == null) throw earlier in your own factory
Example fix
// before AsyncObservable.Generate(0, x => x < 10, null, x => x, Scheduler.Default); // after AsyncObservable.Generate(0, x => x < 10, x => x + 1, x => x, Scheduler.Default);
Defensive patterns
Strategy: validation
Validate before calling
if (iterate == null)
throw new ArgumentNullException(nameof(iterate));
var obs = AsyncObservable.Generate(state, condition, iterate, resultSelector, scheduler); Type guard
static bool HasStep<TState>(Func<TState, TState>? iterate) => iterate != null;
Try / catch
try
{
var obs = AsyncObservable.Generate(state, condition, iterate, selector, scheduler);
}
catch (ArgumentNullException ex) when (ex.ParamName == "iterate")
{
logger.LogError("Missing iterate delegate for scheduled Generate");
} Prevention
- Avoid explicit null literals in five-argument calls
- Resolve iterate from a source guaranteed non-null
- Fail at configuration time, not at observable construction
When it happens
Trigger: Calling AsyncObservable.Generate(initialState, condition, null, resultSelector, scheduler) — null step function while specifying a scheduler, e.g. when building arguments programmatically.
Common situations: A null iterate delegate coming from DI or config; five-argument call where an optional lambda was omitted but null was passed explicitly; refactors that reordered parameters.
Related errors
- Value cannot be null. (Parameter 'resultSelector')
- nameof(scheduler)
- scheduler
- scheduler
- ArgumentNull_Generic
AI-assisted analysis of dotnet/reactive@94b5d5ab91 (2026-09-15).
Data as JSON: /api/errors/12e4454fec585aa6.
Report an issue: GitHub.
Appendix: source
Thrown at AsyncRx.NET/System.Reactive.Async/Linq/Operators/Generate.cs:29
{
public static IAsyncObservable<TResult> Generate<TState, TResult>(TState initialState, Func<TState, bool> condition, Func<TState, TState> iterate, Func<TState, TResult> resultSelector)
{
if (condition == null)
throw new ArgumentNullException(nameof(condition));
if (iterate == null)
throw new ArgumentNullException(nameof(iterate));
if (resultSelector == null)
throw new ArgumentNullException(nameof(resultSelector));
return Create<TResult>(observer => AsyncObserver.Generate(observer, initialState, condition, iterate, resultSelector));
}
public static IAsyncObservable<TResult> Generate<TState, TResult>(TState initialState, Func<TState, bool> condition, Func<TState, TState> iterate, Func<TState, TResult> resultSelector, IAsyncScheduler scheduler)
{
if (condition == null)
throw new ArgumentNullException(nameof(condition));
if (iterate == null)
throw new ArgumentNullException(nameof(iterate));
if (resultSelector == null)
throw new ArgumentNullException(nameof(resultSelector));
if (scheduler == null)
throw new ArgumentNullException(nameof(scheduler));
return Create<TResult>(observer => AsyncObserver.Generate(observer, initialState, condition, iterate, resultSelector, scheduler));
}
public static IAsyncObservable<TResult> Generate<TState, TResult>(TState initialState, Func<TState, ValueTask<bool>> condition, Func<TState, ValueTask<TState>> iterate, Func<TState, ValueTask<TResult>> resultSelector)
{
if (condition == null)
throw new ArgumentNullException(nameof(condition));
if (iterate == null)
throw new ArgumentNullException(nameof(iterate));
if (resultSelector == null)
throw new ArgumentNullException(nameof(resultSelector));
return Create<TResult>(observer => AsyncObserver.Generate(observer, initialState, condition, iterate, resultSelector));View on GitHub (pinned to 94b5d5ab91)