AvaloniaUI/Avalonia · error · ArgumentNullException

Value cannot be null. (Parameter 'onError')

Error message

Value cannot be null. (Parameter 'onError')

What it means

The same AnonymousObserver<T>(onNext, onError, onCompleted) constructor throws ArgumentNullException(nameof(onError)) when the second Action<Exception> is null, again via inline `?? throw`. An error handler is mandatory because unhandled OnError would otherwise be swallowed.

Source

Thrown at src/Avalonia.Base/Reactive/AnonymousObserver.cs:32

    private readonly Action<Exception> _onError;
    private readonly Action _onCompleted;

    public AnonymousObserver(TaskCompletionSource<T> tcs)
    {
        if (tcs is null)
        {
            throw new ArgumentNullException(nameof(tcs));
        }

        _onNext = tcs.SetResult;
        _onError = tcs.SetException;
        _onCompleted = NoOpCompleted;
    }
    
    public AnonymousObserver(Action<T> onNext, Action<Exception> onError, Action onCompleted)
    {
        _onNext = onNext ?? throw new ArgumentNullException(nameof(onNext));
        _onError = onError ?? throw new ArgumentNullException(nameof(onError));
        _onCompleted = onCompleted ?? throw new ArgumentNullException(nameof(onCompleted));
    }

    public AnonymousObserver(Action<T> onNext)
        : this(onNext, ThrowsOnError, NoOpCompleted)
    {
    }

    public AnonymousObserver(Action<T> onNext, Action<Exception> onError)
        : this(onNext, onError, NoOpCompleted)
    {
    }

    public AnonymousObserver(Action<T> onNext, Action onCompleted)
        : this(onNext, ThrowsOnError, onCompleted)
    {
    }

View on GitHub (pinned to 11c5427268)

Solutions

  1. Always pass a non-null Action<Exception> for onError (at minimum log it).
  2. Null-check before constructing if the handler is computed/optional.
  3. Use a static ThrowsOnError or logging fallback so you never silently drop errors.

Example fix

// before
source.Subscribe(new AnonymousObserver<T>(onNext, null, onDone)); // throws

// after
source.Subscribe(new AnonymousObserver<T>(onNext, ex => Log.Error(ex), onDone));
Defensive patterns

Strategy: validation

Validate before calling

if (onError is null) onError = ex => Log.Error(ex);
source.Subscribe(new AnonymousObserver<T>(onNext, onError, onCompleted));

Type guard

bool HasOnError(Action<Exception>? a) => a is not null;

Prevention

When it happens

Trigger: Constructing AnonymousObserver<T> with a non-null onNext but a null onError Action, or calling the (onNext, onError) overload with a null second argument.

Common situations: Forgetting to supply an error handler, or passing a null delegate field, when bridging an observable into callbacks.

Related errors


AI-assisted analysis of AvaloniaUI/Avalonia@11c5427268 (2026-08-13). Data as JSON: /api/errors/a13bb1d81a0dd855. Report an issue: GitHub.