AvaloniaUI/Avalonia · error · ArgumentNullException

items

Error message

items

What it means

ArgumentNullException thrown by AvaloniaList.InsertRange when the items enumerable is null. The method iterates the sequence to insert items and cannot proceed without one.

Source

Thrown at src/Avalonia.Base/Collections/AvaloniaList.cs:364

        /// <param name="item">The item.</param>
        public virtual void Insert(int index, T item)
        {
            Validator?.Validate(item);

            OnMutating();

            _inner.Insert(index, item);
            NotifyAdd(item, index);
        }

        /// <summary>
        /// Inserts multiple items at the specified index.
        /// </summary>
        /// <param name="index">The index.</param>
        /// <param name="items">The items.</param>
        public virtual void InsertRange(int index, IEnumerable<T> items)
        {
            _ = items ?? throw new ArgumentNullException(nameof(items));

            bool willRaiseCollectionChanged = _collectionChanged != null;
            bool hasValidation = Validator is not null;

            if (items is IList list)
            {
                if (list.Count > 0)
                {
                    OnMutating();

                    if (list is ICollection<T> collection)
                    {
                        if (hasValidation)
                        {
                            foreach (T item in collection)
                            {
                                Validator!.Validate(item);
                            }

View on GitHub (pinned to 11c5427268)

Solutions

  1. Pass a non-null enumerable (e.g. Array.Empty<T>() or an empty list) when there is nothing to insert.
  2. Null-check the source before calling InsertRange and skip the call when null.
  3. Fix the upstream producer so it never returns null for a collection.

Example fix

// before
list.InsertRange(0, maybeNullItems);

// after
if (maybeNullItems is not null) list.InsertRange(0, maybeNullItems);
Defensive patterns

Strategy: validation

Validate before calling

if (items is null) return;
list.InsertRange(index, items);

Prevention

When it happens

Trigger: Calling list.InsertRange(index, null) or passing a null IEnumerable<T> from an uninitialized property/method result.

Common situations: A LINQ/select chain yielding null, an optional collection argument forwarded unchecked, or a property that lazily initializes but is accessed before assignment.

Related errors


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