dotnet/reactive · error · ArgumentNullException
Argument cannot be null (Parameter name: observer)
Error message
Argument cannot be null (Parameter name: observer)
What it means
ArgumentNullException (Parameter name: observer) thrown by AsyncObserver.First<TSource>(IAsyncObserver<TSource>) when the downstream observer is null. This factory builds the observer-side logic that takes the first element then completes; it validates the observer before wiring callbacks. It surfaces through First/FirstAsync when internals or custom subscriptions pass a null observer.
Solutions
- In custom operators, validate the observer with ArgumentNullException.ThrowIfNull before calling AsyncObserver.First
- Ensure the caller (Create/SubscribeSafeAsync path) always supplies a non-null observer
- When testing, pass a stub/test observer rather than null
Example fix
// before
public IAsyncObservable<T> MyOp(...) => ... AsyncObserver.First(observer); // observer may be null
// after
public IAsyncObservable<T> MyOp(...)
{
ArgumentNullException.ThrowIfNull(observer);
return ... AsyncObserver.First(observer);
} Defensive patterns
Strategy: validation
Validate before calling
if (observer == null) throw new ArgumentNullException(nameof(observer)); var firstObserver = AsyncObserver.First(observer);
Type guard
bool HasObserver<T>(IAsyncObserver<T>? o) => o is not null;
Try / catch
try
{
var firstObserver = AsyncObserver.First(observer);
}
catch (ArgumentNullException ex) when (ex.ParamName == nameof(observer))
{
throw new InvalidOperationException("Downstream observer missing", ex);
} Prevention
- In custom operators, always validate the observer before composing
- Never construct observers conditionally such that they can be null
- Cover custom operator Subscribe paths with tests that pass a real observer
When it happens
Trigger: Calling AsyncObserver.First(null) directly, or implementing a custom IAsyncObservable/operator that passes a null observer into AsyncObserver.First — typically from a broken SubscribeSafeAsync path or a custom Create overload.
Common situations: Custom operator development where the observer argument is forwarded from a caller that supplied null; unit tests invoking AsyncObserver factories with null arguments.
Related errors
- throw new ArgumentNullException(nameof(observer));
- Value cannot be null. (Parameter 'observer')
- nameof(finallyAction)
- nameof(source)
- nameof(predicate)
AI-assisted analysis of dotnet/reactive@94b5d5ab91 (2026-09-15).
Data as JSON: /api/errors/27c038a20d03f9b9.
Report an issue: GitHub.
Appendix: source
Thrown at AsyncRx.NET/System.Reactive.Async/Linq/Operators/First.cs:55
{
if (source == null)
throw new ArgumentNullException(nameof(source));
if (predicate == null)
throw new ArgumentNullException(nameof(predicate));
return Create(
source,
predicate,
static (source, predicate, observer) => source.SubscribeSafeAsync(AsyncObserver.First(observer, predicate)));
}
}
public partial class AsyncObserver
{
public static IAsyncObserver<TSource> First<TSource>(IAsyncObserver<TSource> observer)
{
if (observer == null)
throw new ArgumentNullException(nameof(observer));
return Create<TSource>(
async x =>
{
await observer.OnNextAsync(x).ConfigureAwait(false);
await observer.OnCompletedAsync().ConfigureAwait(false);
},
observer.OnErrorAsync,
async () =>
{
await observer.OnErrorAsync(new InvalidOperationException("The sequence is empty.")).ConfigureAwait(false);
}
);
}
public static IAsyncObserver<TSource> First<TSource>(IAsyncObserver<TSource> observer, Func<TSource, bool> predicate)
{
if (observer == null)View on GitHub (pinned to 94b5d5ab91)