AvaloniaUI/Avalonia · error · ArgumentNullException

Value cannot be null. (Parameter 'observer')

Error message

Value cannot be null. (Parameter 'observer')

What it means

Thrown by SingleSubscriberObservableBase<T>.Subscribe when the IObserver<T> argument is null. This internal base supports observables that can have exactly one active subscriber (e.g. a single UI-thread property source). It also requires UI-thread access, so the null check happens before the threading check.

Source

Thrown at src/Avalonia.Base/Reactive/SingleSubscriberObservableBase.cs:14

using System;
using Avalonia.Threading;

namespace Avalonia.Reactive
{
    internal abstract class SingleSubscriberObservableBase<T> : IObservable<T>, IDisposable
    {
        private Exception? _error;
        private IObserver<T>? _observer;
        private bool _completed;

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

            if (_observer != null)
            {
                throw new InvalidOperationException("The observable can only be subscribed once.");
            }

            if (_error != null)
            {
                observer.OnError(_error);
            }
            else if (_completed)
            {
                observer.OnCompleted();
            }
            else
            {
                _observer = observer;

View on GitHub (pinned to 11c5427268)

Solutions

  1. Pass a real observer instance or use a Subscribe lambda overload.
  2. Null-check before subscribing and skip the call.
  3. Ensure the observer is constructed before the subscription is initiated.

Example fix

// before
singleSubObs.Subscribe(myObserver); // myObserver is null
// after
if (myObserver != null) singleSubObs.Subscribe(myObserver);
Defensive patterns

Strategy: validation

Validate before calling

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

Type guard

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

Prevention

When it happens

Trigger: Calling a single-subscriber observable's Subscribe(null), or passing an observer reference that is null. Reached internally from Avalonia property/binding code that forgot to construct an observer.

Common situations: A binding adapter passes null when the target is detached but still subscribed; an observer field is read before assignment in a race; refactor left a null observer in a subscription chain.

Related errors


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