dotnet/reactive · error · ArgumentNullException
Value cannot be null. (Parameter 'onCompleted')
Error message
Value cannot be null. (Parameter 'onCompleted')
What it means
Thrown by the notification-based SelectMany(source, onNext, onError, onCompleted) overload when the onCompleted delegate (Func<IObservable<TResult>>) is null. Completion must be mapped to a terminating result sequence, so Rx validates it eagerly and rejects null before any subscription.
Solutions
- Pass a completion factory, e.g. () => Observable.Empty<TResult>(), as the standard completion mapping.
- If completion should emit a final value, use () => Observable.Return(defaultValue).
- Switch to a simpler SelectMany overload if per-notification mapping is not actually needed.
Example fix
// before var q = source.SelectMany(x => Load(x), ex => Observable.Throw<Item>(ex), null); // after var q = source.SelectMany(x => Load(x), ex => Observable.Throw<Item>(ex), () => Observable.Empty<Item>());
Defensive patterns
Strategy: validation
Validate before calling
if (onCompleted is null) onCompleted = static () => Observable.Empty<TResult>(); // or throw before the call
Type guard
static bool CompletionFactoryPresent<TResult>(Func<IObservable<TResult>> onCompleted)
=> onCompleted is not null; Try / catch
try
{
var q = source.SelectMany(onNext, onError, onCompleted);
}
catch (ArgumentNullException ex) when (ex.ParamName == "onCompleted")
{
q = source.SelectMany(onNext, onError, static () => Observable.Empty<TResult>());
} Prevention
- Always pass () => Observable.Empty<TResult>() unless completion must emit a value.
- Review all positional argument lists ending in null after refactors.
- Centralize notification-handler triples in reusable helper methods to avoid omissions.
When it happens
Trigger: Calling Observable.SelectMany(source, onNext, onError, null) — the completion-to-observable factory is null, typically when the developer supplies handlers positionally and stops early.
Common situations: Incomplete refactors adding notification-based SelectMany over an existing two-argument version; misunderstanding that onCompleted is optional; template code copied partially from documentation.
Related errors
- Value cannot be null. (Parameter 'onNext')
- Value cannot be null. (Parameter 'onError')
- Value cannot be null. (Parameter 'func')
- Value cannot be null. (Parameter 'resultSelector')
- new ArgumentNullException(nameof(comparer))
AI-assisted analysis of dotnet/reactive@94b5d5ab91 (2026-09-15).
Data as JSON: /api/errors/1bf56c2ee7b65547.
Report an issue: GitHub.
Appendix: source
Thrown at Rx.NET/Source/src/System.Reactive/Linq/Observable.StandardSequenceOperators.cs:1390
{
if (source == null)
{
throw new ArgumentNullException(nameof(source));
}
if (onNext == null)
{
throw new ArgumentNullException(nameof(onNext));
}
if (onError == null)
{
throw new ArgumentNullException(nameof(onError));
}
if (onCompleted == null)
{
throw new ArgumentNullException(nameof(onCompleted));
}
return s_impl.SelectMany(source, onNext, onError, onCompleted);
}
/// <summary>
/// Projects each notification of an observable sequence to an observable sequence by incorporating the element's index and merges the resulting observable sequences into one observable sequence.
/// </summary>
/// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
/// <typeparam name="TResult">The type of the elements in the projected inner sequences and the elements in the merged result sequence.</typeparam>
/// <param name="source">An observable sequence of notifications to project.</param>
/// <param name="onNext">A transform function to apply to each element; the second parameter of the function represents the index of the source element.</param>
/// <param name="onError">A transform function to apply when an error occurs in the source sequence.</param>
/// <param name="onCompleted">A transform function to apply when the end of the source sequence is reached.</param>
/// <returns>An observable sequence whose elements are the result of invoking the one-to-many transform function corresponding to each notification in the input sequence.</returns>
/// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="onNext"/> or <paramref name="onError"/> or <paramref name="onCompleted"/> is null.</exception>
public static IObservable<TResult> SelectMany<TSource, TResult>(this IObservable<TSource> source, Func<TSource, int, IObservable<TResult>> onNext, Func<Exception, IObservable<TResult>> onError, Func<IObservable<TResult>> onCompleted)
{View on GitHub (pinned to 94b5d5ab91)