dotnet/reactive · error · ArgumentNullException
throw new ArgumentNullException(nameof(resultSelector));
Error message
throw new ArgumentNullException(nameof(resultSelector));
What it means
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.
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.
Example fix
// before Func<int, string> sel = null; var xs = Observable.Generate(0, i => i < 5, i => i + 1, sel, i => TimeSpan.FromSeconds(i)); // after var xs = Observable.Generate(0, i => i < 5, i => i + 1, i => i.ToString(), i => TimeSpan.FromSeconds(i));
Defensive patterns
Strategy: validation
Validate before calling
if (resultSelector is null) throw new InvalidOperationException("resultSelector must be provided before calling Observable.Generate"); Type guard
static bool HasSelector<TState, TResult>(Func<TState, TResult> resultSelector) => resultSelector is not null;
Try / catch
try { var xs = Observable.Generate(0, cond, iter, resultSelector, timeSel); }
catch (ArgumentNullException ex) when (ex.ParamName == "resultSelector") { /* fall back to identity projection */ } Prevention
- 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.
When it happens
Trigger: Calling Observable.Generate(initialState, condition, iterate, resultSelector: null, timeSelector) with Func<TState, TimeSpan> timeSelector — typically a forgotten lambda or a null delegate field.
Common situations: Dynamically composed queries where the projection lambda comes from an optional parameter or DI-resolved factory that returned null.
Related errors
- throw new ArgumentNullException(nameof(iterate));
- throw new ArgumentNullException(nameof(timeSelector));
- ArgumentNullException: comparer
- comparer
- new ArgumentNullException(nameof(comparer))
AI-assisted analysis of dotnet/reactive@94b5d5ab91 (2026-09-15).
Data as JSON: /api/errors/f80b51e8036c5f7d.
Report an issue: GitHub.
Appendix: source
Thrown at Rx.NET/Source/src/System.Reactive/Linq/Observable.Time.cs:637
/// <param name="resultSelector">Selector function for results produced in the sequence.</param>
/// <param name="timeSelector">Time selector function to control the speed of values being produced each iteration.</param>
/// <returns>The generated sequence.</returns>
/// <exception cref="ArgumentNullException"><paramref name="condition"/> or <paramref name="iterate"/> or <paramref name="resultSelector"/> or <paramref name="timeSelector"/> is null.</exception>
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)
{
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 (timeSelector == null)
{
throw new ArgumentNullException(nameof(timeSelector));
}
return s_impl.Generate(initialState, condition, iterate, resultSelector, timeSelector);
}
/// <summary>
/// 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.
/// </summary>
/// <typeparam name="TState">The type of the state used in the generator loop.</typeparam>
/// <typeparam name="TResult">The type of the elements in the produced sequence.</typeparam>
/// <param name="initialState">Initial state.</param>
/// <param name="condition">Condition to terminate generation (upon returning false).</param>
/// <param name="iterate">Iteration step function.</param>View on GitHub (pinned to 94b5d5ab91)