AvaloniaUI/Avalonia · error · ArgumentNullException

dictionary

Error message

dictionary

What it means

ArgumentNullException thrown by the AvaloniaDictionary copy constructor when the source IDictionary is null. The constructor needs a non-null source dictionary to seed its inner Dictionary.

Source

Thrown at src/Avalonia.Base/Collections/AvaloniaDictionary.cs:48

        /// Initializes a new instance of the <see cref="AvaloniaDictionary{TKey, TValue}"/> class.
        /// </summary>
        public AvaloniaDictionary(int capacity)
        {
            _inner = new Dictionary<TKey, TValue>(capacity);
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="AvaloniaDictionary{TKey, TValue}"/> class using an IDictionary.
        /// </summary>
        public AvaloniaDictionary(IDictionary<TKey, TValue> dictionary, IEqualityComparer<TKey>? comparer = null)
        {
            if (dictionary != null)
            {
                _inner = new Dictionary<TKey, TValue>(dictionary, comparer ?? EqualityComparer<TKey>.Default);
            }
            else
            {
                throw new ArgumentNullException(nameof(dictionary));
            }
        }

        /// <summary>
        /// Occurs when the collection changes.
        /// </summary>
        public event NotifyCollectionChangedEventHandler? CollectionChanged;

        /// <summary>
        /// Raised when a property on the collection changes.
        /// </summary>
        public event PropertyChangedEventHandler? PropertyChanged;

        /// <inheritdoc cref="ICollection{T}.Count"/>
        public int Count => _inner.Count;

        /// <inheritdoc cref="ICollection{T}.IsReadOnly"/>
        public bool IsReadOnly => false;

View on GitHub (pinned to 11c5427268)

Solutions

  1. Pass a non-null source dictionary, or use the parameterless constructor when you have no source.
  2. Null-check the source before constructing: 'new AvaloniaDictionary<TKey,TValue>(src ?? new Dictionary<TKey,TValue>())'.
  3. Coalesce upstream so the caller never hands null to the constructor.

Example fix

// before
var dict = new AvaloniaDictionary<string,int>(maybeNull);

// after
var dict = maybeNull is null
    ? new AvaloniaDictionary<string,int>()
    : new AvaloniaDictionary<string,int>(maybeNull);
Defensive patterns

Strategy: validation

Validate before calling

if (source is null) throw new ArgumentNullException(nameof(source));
var dict = new AvaloniaDictionary<TKey,TValue>(source);

Prevention

When it happens

Trigger: Calling 'new AvaloniaDictionary<TKey,TValue>(null)' or passing a null reference from a variable/expression to the dictionary constructor overload that accepts IDictionary<TKey,TValue>.

Common situations: A factory or mapper method that receives an optional/null source collection and forwards it without a null check; deserialization or initialization where the source map has not been populated yet.

Related errors


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