dotnet/reactive · error · ArgumentNullException
observer
Error message
observer
What it means
ListObservable's Subscribe(IObserver<object>) replays recorded notifications via an internal subject and throws ArgumentNullException when 'observer' is null, because there is no meaningful subscription target.
Solutions
- Pass a valid IObserver<object> (e.g. AnonymousObserver) instead of null
- Null-check the observer before subscribing
- Fix the code path that produced a null observer
Example fix
// before listObservable.Subscribe(maybeObserver); // may be null // after var observer = maybeObserver ?? new AnonymousObserver<object>(x => Console.WriteLine(x)); listObservable.Subscribe(observer);
Defensive patterns
Strategy: validation
Validate before calling
if (observer == null) throw new ArgumentNullException(nameof(observer)); listObservable.Subscribe(observer);
Type guard
static bool IsValidObserver(IObserver<object> o) => o != null;
Try / catch
try { listObservable.Subscribe(observer); } catch (ArgumentNullException) { /* null observer */ } Prevention
- Create observers explicitly (AnonymousObserver) rather than from nullable sources
- Null-check observers in subscription wiring code
- Avoid factory helpers that can return null observers
When it happens
Trigger: Calling listObservable.Subscribe(null), typically when the observer comes from a failed lookup, an uninitialized field, or a factory returning null.
Common situations: Test code building observers conditionally; a refactoring replaced a real observer with a null-returning helper; event-wiring code where the handler was never attached.
Related errors
- Value cannot be null. (Parameter 'onNext')
- Value cannot be null. (Parameter 'onError')
- Value cannot be null. (Parameter 'onCompleted')
- source
- onError
AI-assisted analysis of dotnet/reactive@94b5d5ab91 (2026-09-15).
Data as JSON: /api/errors/53c91d792290f1e6.
Report an issue: GitHub.
Appendix: source
Thrown at Rx.NET/Source/src/System.Reactive/ListObservable.cs:204
public IEnumerator<T> GetEnumerator()
{
Wait();
return _results.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
/// <summary>
/// Subscribes an observer to the ListObservable which will be notified upon completion.
/// </summary>
/// <param name="observer">The observer to send completion or error messages to.</param>
/// <returns>The disposable resource that can be used to unsubscribe.</returns>
/// <exception cref="ArgumentNullException"><paramref name="observer"/> is <c>null</c>.</exception>
public IDisposable Subscribe(IObserver<object> observer)
{
if (observer == null)
{
throw new ArgumentNullException(nameof(observer));
}
return StableCompositeDisposable.Create(_subscription, _subject.Subscribe(observer));
}
}
}
View on GitHub (pinned to 94b5d5ab91)