dotnet/reactive · error · ArgumentNullException
Value cannot be null. (Parameter 'onCompletedAsync')
Error message
Value cannot be null. (Parameter 'onCompletedAsync')
What it means
The AsyncObserver constructor null-checks all three delegates; onCompletedAsync was null, so it throws ArgumentNullException. Without a completion delegate the observer could not signal graceful end-of-stream, so construction fails at line 19.
Solutions
- Supply a completion handler, e.g. () => { Console.WriteLine("completed"); return default; } or a no-op () => default.
- Use AsyncObserver.Create or the SubscribeAsync extension overloads instead of constructing AsyncObserver directly.
- Model truly-never-completing sequences with a no-op delegate rather than null.
- Centralize observer creation in one helper that supplies default no-op delegates so nulls never reach the constructor.
Example fix
// before var observer = new AsyncObserver<int>(x => default, ex => default, null); // after var observer = new AsyncObserver<int>(x => default, ex => default, () => default);
Defensive patterns
Strategy: validation
Validate before calling
if (onNextAsync == null || onErrorAsync == null || onCompletedAsync == null)
throw new ArgumentException("AsyncObserver requires non-null onNextAsync, onErrorAsync, and onCompletedAsync delegates.");
var observer = new AsyncObserver<T>(onNextAsync, onErrorAsync, onCompletedAsync); Try / catch
try
{
var observer = new AsyncObserver<T>(onNextAsync, onErrorAsync, onCompletedAsync);
}
catch (ArgumentNullException ex) when (ex.ParamName == "onCompletedAsync")
{
observer = new AsyncObserver<T>(onNextAsync, onErrorAsync, () => default);
} Prevention
- Supply () => default for never-completing streams instead of null.
- Use AsyncObserver.Create or SubscribeAsync helpers to avoid direct construction.
- Build observers through a single factory that applies no-op defaults for missing handlers.
When it happens
Trigger: new AsyncObserver<T>(onNextAsync, onErrorAsync, null) — constructing with a null completion delegate.
Common situations: Assuming completion is optional for infinite streams and passing null; test scaffolding that fills in next/error but not completed; factory code plumbing through user-supplied delegates without defaults.
Related errors
- Value cannot be null. (Parameter 'onNextAsync')
- Value cannot be null. (Parameter 'onErrorAsync')
- Value cannot be null. (Parameter 'context')
- Value cannot be null. (Parameter 'disposable')
- Value cannot be null. (Parameter 'begin')
AI-assisted analysis of dotnet/reactive@94b5d5ab91 (2026-09-15).
Data as JSON: /api/errors/67e666177826b1ba.
Report an issue: GitHub.
Appendix: source
Thrown at AsyncRx.NET/System.Reactive.Async/AsyncObserver.cs:19
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT License.
// See the LICENSE file in the project root for more information.
using System.Threading.Tasks;
namespace System.Reactive
{
public class AsyncObserver<T> : AsyncObserverBase<T>
{
private readonly Func<T, ValueTask> _onNextAsync;
private readonly Func<Exception, ValueTask> _onErrorAsync;
private readonly Func<ValueTask> _onCompletedAsync;
public AsyncObserver(Func<T, ValueTask> onNextAsync, Func<Exception, ValueTask> onErrorAsync, Func<ValueTask> onCompletedAsync)
{
_onNextAsync = onNextAsync ?? throw new ArgumentNullException(nameof(onNextAsync));
_onErrorAsync = onErrorAsync ?? throw new ArgumentNullException(nameof(onErrorAsync));
_onCompletedAsync = onCompletedAsync ?? throw new ArgumentNullException(nameof(onCompletedAsync));
}
protected override ValueTask OnCompletedAsyncCore() => _onCompletedAsync();
protected override ValueTask OnErrorAsyncCore(Exception error) => _onErrorAsync(error ?? throw new ArgumentNullException(nameof(error)));
protected override ValueTask OnNextAsyncCore(T value) => _onNextAsync(value);
}
}
View on GitHub (pinned to 94b5d5ab91)