dotnet/reactive · error · ArgumentNullException
ArgumentNullException (default message: Value cannot be…
Error message
ArgumentNullException (default message: Value cannot be null. (Parameter 'source'))
What it means
Scan(source, seed, accumulator) throws ArgumentNullException when the source sequence is null. Rx operators validate arguments synchronously at composition time so bad inputs surface at the call site, not mid-stream.
Solutions
- Guarantee a non-null source before calling Scan.
- Replace null with an empty or default sequence when absence is valid.
- Fix the upstream factory that returned null.
- Wrap the composition in argument validation with a clearer domain message.
Example fix
// before var running = (GetStream() as IObservable<int>).Scan(0, (acc, x) => acc + x); // after var src = GetStream() ?? Observable.Empty<int>(); var running = src.Scan(0, (acc, x) => acc + x);
Defensive patterns
Strategy: validation
Validate before calling
if (source is null) throw new ArgumentNullException(nameof(source)); var running = source.Scan(seed, (acc, x) => acc + x);
Type guard
bool HasStream<TSource>(IObservable<TSource>? s) => s is not null;
Try / catch
try { result = src.Scan(seed, acc); }
catch (ArgumentNullException ex) when (ex.ParamName == "source") { result = Observable.Empty<TAccumulate>(); } Prevention
- Return Observable.Return/Empty instead of null from sequence providers.
- Coalesce nullable sources at composition time.
- Run composition inside a checked factory that validates inputs once.
When it happens
Trigger: Calling Observable.Scan(source, seed, accumulator) with a null `source` (line 709 check, before the accumulator check).
Common situations: Scan chained onto a nullable result of a repository/API call returning IObservable<T> or null; DI wiring where a sequence dependency is unregistered.
Related errors
- ArgumentNullException (default message: Value cannot be…
- ArgumentNullException (default message: Value cannot be…
- ArgumentNullException: dispatcher
- ArgumentNullException
- ArgumentNullException: resultSelector
AI-assisted analysis of dotnet/reactive@94b5d5ab91 (2026-09-15).
Data as JSON: /api/errors/c3f84e5c09ebd79e.
Report an issue: GitHub.
Appendix: source
Thrown at Rx.NET/Source/src/System.Reactive/Linq/Observable.Single.cs:709
#region + Scan +
/// <summary>
/// Applies an accumulator function over an observable sequence and returns each intermediate result. The specified seed value is used as the initial accumulator value.
/// For aggregation behavior with no intermediate results, see <see cref="Observable.Aggregate{TSource, Accumulate}"/>.
/// </summary>
/// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
/// <typeparam name="TAccumulate">The type of the result of the aggregation.</typeparam>
/// <param name="source">An observable sequence to accumulate over.</param>
/// <param name="seed">The initial accumulator value.</param>
/// <param name="accumulator">An accumulator function to be invoked on each element.</param>
/// <returns>An observable sequence containing the accumulated values.</returns>
/// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="accumulator"/> is null.</exception>
public static IObservable<TAccumulate> Scan<TSource, TAccumulate>(this IObservable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, TAccumulate> accumulator)
{
if (source == null)
{
throw new ArgumentNullException(nameof(source));
}
if (accumulator == null)
{
throw new ArgumentNullException(nameof(accumulator));
}
return s_impl.Scan(source, seed, accumulator);
}
/// <summary>
/// Applies an accumulator function over an observable sequence and returns each intermediate result.
/// For aggregation behavior with no intermediate results, see <see cref="Observable.Aggregate{TSource}"/>.
/// </summary>
/// <typeparam name="TSource">The type of the elements in the source sequence and the result of the aggregation.</typeparam>
/// <param name="source">An observable sequence to accumulate over.</param>
/// <param name="accumulator">An accumulator function to be invoked on each element.</param>
/// <returns>An observable sequence containing the accumulated values.</returns>View on GitHub (pinned to 94b5d5ab91)