dotnet/reactive · error · ArgumentNullException
nameof(observer)
Error message
nameof(observer)
What it means
This is an ArgumentNullException thrown by AsyncObserver.ElementAtOrDefault when the observer argument is null. The factory guards its arguments so that a null observer fails fast at composition time rather than corrupting the subscription later. Null is never a valid observer here.
Solutions
- Pass a valid IAsyncObserver<TSource> (from AsyncObserver.Create or an upstream operator)
- Trace why the observer variable was null and fix that construction path
- Assert non-null in dev builds before composing the pipeline
Example fix
// before var op = AsyncObserver.ElementAtOrDefault(observer, 1); // observer null // after observer ??= AsyncObserver.Create<TSource>(...); var op = AsyncObserver.ElementAtOrDefault(observer, 1);
Defensive patterns
Strategy: validation
Validate before calling
if (observer is null) throw new ArgumentNullException(nameof(observer)); var op = AsyncObserver.ElementAtOrDefault(observer, index);
Type guard
bool IsValidObserver<TSource>(IAsyncObserver<TSource> o) => o is not null;
Try / catch
try { var op = AsyncObserver.ElementAtOrDefault(observer, index); }
catch (ArgumentNullException ex) when (ex.ParamName == "observer") { /* create/assign a real observer */ } Prevention
- Create observers via AsyncObserver.Create before composing operators
- Avoid nullable observer members; initialize them eagerly
- Unit-test pipeline builders for null-observer paths
When it happens
Trigger: Calling AsyncObserver.ElementAtOrDefault<TSource>(null, index); typical when the observer is the result of a conditional chain or an uninitialized member at pipeline-build time.
Common situations: Hand-assembling async observer pipelines in tests; passing the result of a method that can return null; ordering mistakes where the observer is created after the operator call.
Related errors
- Value cannot be null. (Parameter 'observer')
- nameof(observer)
- Value cannot be null. (Parameter 'observer')
- Value cannot be null. (Parameter 'source')
- Value cannot be null. (Parameter 'disposable1')
AI-assisted analysis of dotnet/reactive@94b5d5ab91 (2026-09-15).
Data as JSON: /api/errors/6019b3cb694aa2a5.
Report an issue: GitHub.
Appendix: source
Thrown at AsyncRx.NET/System.Reactive.Async/Linq/Operators/ElementAtOrDefault.cs:28
{
if (source == null)
throw new ArgumentNullException(nameof(source));
if (index < 0)
throw new ArgumentOutOfRangeException(nameof(index));
return Create(
source,
index,
static (source, index, observer) => source.SubscribeSafeAsync(AsyncObserver.ElementAtOrDefault(observer, index)));
}
}
public partial class AsyncObserver
{
public static IAsyncObserver<TSource> ElementAtOrDefault<TSource>(IAsyncObserver<TSource> observer, int index)
{
if (observer == null)
throw new ArgumentNullException(nameof(observer));
if (index < 0)
throw new ArgumentOutOfRangeException(nameof(index));
return Create<TSource>(
async x =>
{
if (index-- == 0)
{
await observer.OnNextAsync(x).ConfigureAwait(false);
await observer.OnCompletedAsync().ConfigureAwait(false);
}
},
observer.OnErrorAsync,
async () =>
{
await observer.OnNextAsync(default).ConfigureAwait(false);
await observer.OnCompletedAsync().ConfigureAwait(false);
}View on GitHub (pinned to 94b5d5ab91)