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
- Pass a real observer instance or use a Subscribe lambda overload.
- Null-check before subscribing and skip the call.
- 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
- Subscribe on the UI thread with a real observer (the base also calls Dispatcher.UIThread.VerifyAccess).
- Do not reuse a null observer reference from a detached binding.
- Construct the observer at the subscription site.
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
- Value cannot be null. (Parameter 'observer')
- collection
- Value cannot be null. (Parameter 'tcs')
- Value cannot be null. (Parameter 'onNext')
- Value cannot be null. (Parameter 'onError')
AI-assisted analysis of AvaloniaUI/Avalonia@11c5427268 (2026-08-13).
Data as JSON: /api/errors/aee69078160045fe.
Report an issue: GitHub.