dotnet/reactive · error · ArgumentNullException
observer
Error message
observer
What it means
The Subscribe(source, observer, token) extension throws ArgumentNullException when the IObserver<T> argument is null. Rx validates the observer upfront because a null observer would make it impossible to deliver OnNext/OnError/OnCompleted notifications.
Solutions
- Pass a valid IObserver<T> instance; if you only need callbacks, use the Action-based overloads or AnonymousObserver.
- Fix the factory/DI registration that produced a null observer.
- Guard with a null check and a contextual exception before subscribing.
- Prefer the observer-returning overload (Subscribe returns IDisposable) once a non-null observer exists.
Example fix
// before
IObserver<int> obs = observers.TryGet("agg"); // may be null
source.Subscribe(obs, token);
// after
IObserver<int> obs = observers.TryGet("agg") ?? new AnonymousObserver<int>(_ => { });
if (obs == null) throw new InvalidOperationException("agg observer not built yet");
source.Subscribe(obs, token); Defensive patterns
Strategy: validation
Validate before calling
if (observer is null) throw new InvalidOperationException("observer not constructed before Subscribe"); Type guard
static bool HasObserver<T>(IObserver<T> observer) => observer is not null;
Try / catch
try { source.Subscribe(observer, token); }
catch (ArgumentNullException ex) when (ex.ParamName == "observer")
{ /* fall back to Action-based overloads */ } Prevention
- Prefer Action-based Subscribe overloads to avoid hand-rolled IObserver implementations
- Register observers in DI before anything subscribes to them
- Check TryGet-style lookups for null before use
When it happens
Trigger: Calling source.Subscribe(observer, token) with a null IObserver<T> — e.g. an unassigned observer field, a custom observer factory returning null, or a cast/lookup that failed.
Common situations: Custom IObserver implementations not yet constructed before wiring up, DI container returning null for an observer dependency, or passing the result of a conditional that resolved to null.
Related errors
- nameof(observer)
- Argument cannot be null (Parameter name: observer)
- ArgumentNullException (default message: Value cannot be…
- ArgumentNullException (default message: Value cannot be…
- ArgumentNullException (default message: Value cannot be…
AI-assisted analysis of dotnet/reactive@94b5d5ab91 (2026-09-15).
Data as JSON: /api/errors/16e038aed4a12363.
Report an issue: GitHub.
Appendix: source
Thrown at Rx.NET/Source/src/System.Reactive/Observable.Extensions.cs:189
/// <summary>
/// Subscribes an observer to an observable sequence, using a <see cref="CancellationToken"/> to support unsubscription.
/// </summary>
/// <typeparam name="T">The type of the elements in the source sequence.</typeparam>
/// <param name="source">Observable sequence to subscribe to.</param>
/// <param name="observer">Observer to subscribe to the sequence.</param>
/// <param name="token">CancellationToken that can be signaled to unsubscribe from the source sequence.</param>
/// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="observer"/> is <c>null</c>.</exception>
public static void Subscribe<T>(this IObservable<T> source, IObserver<T> observer, CancellationToken token)
{
if (source == null)
{
throw new ArgumentNullException(nameof(source));
}
if (observer == null)
{
throw new ArgumentNullException(nameof(observer));
}
source.Subscribe_(observer, token);
}
/// <summary>
/// Subscribes to the observable sequence without specifying any handlers, using a <see cref="CancellationToken"/> to support unsubscription.
/// This method can be used to evaluate the observable sequence for its side-effects only.
/// </summary>
/// <typeparam name="T">The type of the elements in the source sequence.</typeparam>
/// <param name="source">Observable sequence to subscribe to.</param>
/// <param name="token">CancellationToken that can be signaled to unsubscribe from the source sequence.</param>
/// <exception cref="ArgumentNullException"><paramref name="source"/> is <c>null</c>.</exception>
public static void Subscribe<T>(this IObservable<T> source, CancellationToken token)
{
if (source == null)
{
throw new ArgumentNullException(nameof(source));View on GitHub (pinned to 94b5d5ab91)