dotnet/reactive · error · ArgumentNullException
nameof(source)
Error message
nameof(source)
What it means
The message is "Value cannot be null. (Parameter 'source')" thrown at Observable.Blocking.cs:563: Wait<TSource> validates that the IObservable<TSource> is non-null before blocking the calling thread until the sequence emits its single value. A null observable cannot be subscribed to, and Wait's contract (block and return the last element, throwing InvalidOperationException if empty) only makes sense on a real sequence.
Solutions
- Null-check before waiting: if (obs != null) { var v = obs.Wait(); }
- Fix the producer to return Observable.Empty<TSource>() (Wait will then surface the documented empty-sequence InvalidOperationException) or a real sequence instead of null.
- Ensure initialization ordering: the observable-producing component must be initialized before any Wait call.
Example fix
// before
var result = _results.Wait(); // _results is null during startup
// after
if (_results == null)
{
throw new InvalidOperationException("Results pipeline not initialized");
}
var result = _results.Wait(); Defensive patterns
Strategy: validation
Validate before calling
if (source is null) throw new ArgumentNullException(nameof(source)); var result = source.Wait();
Type guard
bool CanWait<T>(IObservable<T>? source) => source is not null;
Try / catch
try
{
var result = source.Wait();
}
catch (ArgumentNullException ex) when (ex.ParamName == "source")
{
// source was null; handle or rethrow with context
throw new InvalidOperationException("Result sequence not initialized", ex);
} Prevention
- Ensure the observable-producing component is initialized before any Wait call.
- Remember Wait blocks the thread: on a valid sequence also be prepared for timeouts/empty-source InvalidOperationException.
- Enable nullable reference types to catch uninitialized observable fields at compile time.
When it happens
Trigger: Calling Observable.Wait<TSource>(null) — e.g. waiting on a field/property holding the observable before it has been assigned, or on the result of an API that returned null instead of a sequence.
Common situations: Test code waiting on a subject/task-observable that failed to initialize; bridging synchronous code to Rx where the observable is produced by another component that returned null on failure; startup ordering bugs where Wait runs before the producer pipeline is built.
Related errors
- Value cannot be null. (Parameter 'onNextAsync')
- Value cannot be null. (Parameter 'onErrorAsync')
- Value cannot be null. (Parameter 'onCompletedAsync')
- Value cannot be null. (Parameter 'second')
- Value cannot be null. (Parameter 'sources')
AI-assisted analysis of dotnet/reactive@94b5d5ab91 (2026-09-15).
Data as JSON: /api/errors/856d73b2f98c305d.
Report an issue: GitHub.
Appendix: source
Thrown at Rx.NET/Source/src/System.Reactive/Linq/Observable.Blocking.cs:563
#endregion
#region + Wait +
/// <summary>
/// Waits for the observable sequence to complete and returns the last element of the sequence.
/// If the sequence terminates with an OnError notification, the exception is thrown.
/// </summary>
/// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
/// <param name="source">Source observable sequence.</param>
/// <returns>The last element in the observable sequence.</returns>
/// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
/// <exception cref="InvalidOperationException">The source sequence is empty.</exception>
public static TSource Wait<TSource>(this IObservable<TSource> source)
{
if (source == null)
{
throw new ArgumentNullException(nameof(source));
}
return s_impl.Wait(source);
}
#endregion
}
}
View on GitHub (pinned to 94b5d5ab91)