AvaloniaUI/Avalonia · error · ArgumentNullException

collection

Error message

collection

What it means

ArgumentNullException thrown by NotifyCollectionChangedExtensions.GetWeakCollectionChangedObservable when the source collection is null. The extension creates a WeakCollectionChangedObservable holding a WeakReference to the collection and requires a live target.

Source

Thrown at src/Avalonia.Base/Collections/NotifyCollectionChangedExtensions.cs:18

using System;
using System.Collections.Specialized;
using Avalonia.Reactive;
using Avalonia.Utilities;

namespace Avalonia.Collections
{
    public static class NotifyCollectionChangedExtensions
    {
        /// <summary>
        /// Gets a weak observable for the CollectionChanged event.
        /// </summary>
        /// <param name="collection">The collection.</param>
        /// <returns>An observable.</returns>
        public static IObservable<NotifyCollectionChangedEventArgs> GetWeakCollectionChangedObservable(
            this INotifyCollectionChanged collection)
        {
            _ = collection ?? throw new ArgumentNullException(nameof(collection));

            return new WeakCollectionChangedObservable(new WeakReference<INotifyCollectionChanged>(collection));
        }

        /// <summary>
        /// Subscribes to the CollectionChanged event using a weak subscription.
        /// </summary>
        /// <param name="collection">The collection.</param>
        /// <param name="handler">
        /// An action called when the collection event is raised.
        /// </param>
        /// <returns>A disposable used to terminate the subscription.</returns>
        public static IDisposable WeakSubscribe(
            this INotifyCollectionChanged collection, 
            NotifyCollectionChangedEventHandler handler)
        {
            _ = collection ?? throw new ArgumentNullException(nameof(collection));
            _ = handler ?? throw new ArgumentNullException(nameof(handler));

View on GitHub (pinned to 11c5427268)

Solutions

  1. Ensure the collection is non-null before calling the extension (guard the call site).
  2. Initialize the collection property before wiring subscriptions.
  3. Null-check and skip/return an empty observable when the source is absent.

Example fix

// before
var obs = maybeNullCollection.GetWeakCollectionChangedObservable();

// after
var obs = maybeNullCollection is null
    ? Observable.Empty<NotifyCollectionChangedEventArgs>()
    : maybeNullCollection.GetWeakCollectionChangedObservable();
Defensive patterns

Strategy: validation

Validate before calling

if (collection is null) throw new ArgumentNullException(nameof(collection));
var obs = collection.GetWeakCollectionChangedObservable();

Type guard

static bool IsSubscribable(INotifyCollectionChanged? c) => c is not null;

Prevention

When it happens

Trigger: Calling collection.GetWeakCollectionChangedObservable() where the 'collection' receiver (the 'this' argument) is null; passing null explicitly as the INotifyCollectionChanged argument.

Common situations: A collection property that is null when subscribed to (lazy/not-yet-initialized); a binding/reactive chain that forwards a null collection reference into the weak-subscribe path.

Related errors


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