dotnet/reactive · error · ArgumentNullException
Value cannot be null. (Parameter 'onCompletedAsync')
Error message
Value cannot be null. (Parameter 'onCompletedAsync')
What it means
ArgumentNullException from CreateUnsafe for onCompletedAsync being null; the message exposes 'Value cannot be null. (Parameter onCompletedAsync)'. The completion handler is mandatory in the Unsafe overload, which forwards delegates straight to UnsafeAsyncObserver<T>. The throw happens synchronously, before any observer callbacks can run.
Solutions
- Pass a non-null onCompletedAsync, e.g. () => default for no-op completion.
- Validate all three delegates before calling CreateUnsafe.
- Use the safe Create(onNextAsync) overload when custom completion/error handling is unnecessary.
Example fix
// before AsyncObserver.CreateUnsafe<int>(onNext, onError, null); // after AsyncObserver.CreateUnsafe<int>(onNext, onError, () => default);
Defensive patterns
Strategy: validation
Validate before calling
if (onCompletedAsync is null) onCompletedAsync = () => default; var observer = AsyncObserver.CreateUnsafe(onNextAsync, onErrorAsync, onCompletedAsync);
Try / catch
try
{
var observer = AsyncObserver.CreateUnsafe(onNextAsync, onErrorAsync, onCompletedAsync);
}
catch (ArgumentNullException ex) when (ex.ParamName == "onCompletedAsync")
{
var observer = AsyncObserver.CreateUnsafe(onNextAsync, onErrorAsync, () => default);
} Prevention
- Use () => default for no-op completion in unsafe observer construction.
- Never leave completion-handler fields uninitialized; default them at declaration.
- Validate the full delegate triple in a shared helper before any Create/CreateUnsafe call.
- Cover internal operators with tests that pass each handler as null to verify fail-fast behavior.
When it happens
Trigger: Calling CreateUnsafe<T>(onNext, onError, null) with a null completion delegate, e.g. a () => default handler stored in a field that was never initialized.
Common situations: Internal operators building observers from nullable handler fields; hand-written operators or tests omitting the completion handler; refactoring that dropped the () => default argument.
Related errors
- ArgumentNullException
- Value cannot be null. (Parameter 'observer')
- Value cannot be null. (Parameter 'onErrorAsync')
- Value cannot be null. (Parameter 'onCompletedAsync')
- Value cannot be null. (Parameter 'error')
AI-assisted analysis of dotnet/reactive@94b5d5ab91 (2026-09-15).
Data as JSON: /api/errors/34f613d234ce00c5.
Report an issue: GitHub.
Appendix: source
Thrown at AsyncRx.NET/System.Reactive.Async/Linq/AsyncObserver.cs:42
{
if (onNextAsync == null)
throw new ArgumentNullException(nameof(onNextAsync));
if (onErrorAsync == null)
throw new ArgumentNullException(nameof(onErrorAsync));
if (onCompletedAsync == null)
throw new ArgumentNullException(nameof(onCompletedAsync));
return new AsyncObserver<T>(onNextAsync, onErrorAsync, onCompletedAsync);
}
internal static IAsyncObserver<T> CreateUnsafe<T>(Func<T, ValueTask> onNextAsync, Func<Exception, ValueTask> onErrorAsync, Func<ValueTask> onCompletedAsync)
{
if (onNextAsync == null)
throw new ArgumentNullException(nameof(onNextAsync));
if (onErrorAsync == null)
throw new ArgumentNullException(nameof(onErrorAsync));
if (onCompletedAsync == null)
throw new ArgumentNullException(nameof(onCompletedAsync));
return new UnsafeAsyncObserver<T>(onNextAsync, onErrorAsync, onCompletedAsync);
}
}
}
View on GitHub (pinned to 94b5d5ab91)