AvaloniaUI/Avalonia · error · ArgumentNullException

Value cannot be null. (Parameter 'observer')

Error message

Value cannot be null. (Parameter 'observer')

What it means

Thrown by LightweightObservableBase<T>.Subscribe when the IObserver<T> argument is null. LightweightObservableBase is an internal base for low-overhead observables that live on the UI thread. Subscribe stores the observer in a volatile list, so a null observer would NRE later during dispatch; the guard fails fast.

Source

Thrown at src/Avalonia.Base/Reactive/LightweightObservableBase.cs:26

    /// <summary>
    /// Lightweight base class for observable implementations.
    /// </summary>
    /// <typeparam name="T">The observable type.</typeparam>
    /// <remarks>
    /// ObservableBase{T} is rather heavyweight in terms of allocations and memory
    /// usage. This class provides a more lightweight base for some internal observable types
    /// in the Avalonia framework.
    /// </remarks>
    internal abstract class LightweightObservableBase<T> : IObservable<T>
    {
        private Exception? _error;
        private List<IObserver<T>>? _observers = new List<IObserver<T>>();

        public bool HasObservers => _observers?.Count > 0;

        public IDisposable Subscribe(IObserver<T> observer)
        {
            _ = observer ?? throw new ArgumentNullException(nameof(observer));

            //Dispatcher.UIThread.VerifyAccess();

            var first = false;

            for (; ; )
            {
                if (Volatile.Read(ref _observers) == null)
                {
                    if (_error != null)
                    {
                        observer.OnError(_error);
                    }
                    else
                    {
                        observer.OnCompleted();
                    }

View on GitHub (pinned to 11c5427268)

Solutions

  1. Pass a concrete observer instance (e.g. AnonymousObserver or a lambda via Subscribe(onNext, onError, onCompleted)).
  2. Null-check the observer before subscribing.
  3. If the source can legitimately be null, skip the subscription or substitute an no-op observer.

Example fix

// before
obs.Subscribe(observer); // observer is null
// after
obs.Subscribe(observer ?? new AnonymousObserver<T>(_ => { }));
Defensive patterns

Strategy: validation

Validate before calling

if (observer is null) throw new ArgumentNullException(nameof(observer));
obs.Subscribe(observer);

Type guard

static bool IsValidObserver<T>(IObserver<T>? o) => o is not null;

Prevention

When it happens

Trigger: Calling observable.Subscribe(null) directly, or passing an observer variable that resolved to null. Also reachable through binding/style machinery that wires up a null observer when a property source is unavailable.

Common situations: A reactive binding source returns null under a transient state; a custom observer field is not yet constructed when the subscription is made; subscribing via an overload that defaulted an observer to null.

Related errors


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