stride3d/stride · error · ArgumentNullException

Destination array cannot be null.

Error message

Destination array cannot be null.

What it means

Deque<T>'s non-generic System.Collections.ICollection.CopyTo(Array array, int index) is used when the deque is accessed through the legacy non-generic collection interface (e.g. reflection-based serializers, legacy collection interop). It throws ArgumentNullException('Destination array cannot be null.') when the supplied Array is null, before the range check and element copying.

Solutions

  1. Ensure a non-null Array is allocated before calling the non-generic CopyTo.
  2. Prefer the generic ICollection<T>.CopyTo (or ToArray) which is compile-time type-safe.
  3. Null-check the destination before dispatching through the ICollection interface.

Example fix

// before
((ICollection)deque).CopyTo(destination, 0); // destination may be null

// after
if (destination != null)
    ((ICollection)deque).CopyTo(destination, 0);
Defensive patterns

Strategy: type-guard

Validate before calling

if (array == null)
    throw new ArgumentException("Destination array must be allocated before CopyTo");
((ICollection)deque).CopyTo(array, index);

Type guard

static bool CanCopyTo(Array? array, int index, int count) =>
    array is not null && index >= 0 && count <= array.Length - index;

Try / catch

try
{
    ((ICollection)deque).CopyTo(array, index);
}
catch (ArgumentNullException ex) when (ex.ParamName == "array")
{
    logger.Error("Non-generic CopyTo called with a null destination array");
}

Prevention

When it happens

Trigger: ((ICollection)deque).CopyTo(null, 0); calling CopyTo on an ICollection-typed variable with a null destination; frameworks invoking the non-generic interface member with an uninitialized buffer.

Common situations: Legacy collection APIs (e.g. code written against ArrayList-era patterns); serializers that use the non-generic ICollection contract; cast-through-object code paths that lose type information.

Related errors


AI-assisted analysis of stride3d/stride@96fad776d2 (2026-09-14). Data as JSON: /api/errors/ecd6354bcfc88d8d. Report an issue: GitHub.

Appendix: source

Thrown at sources/core/Stride.Core/Collections/Dequeue.cs:357

    }

    object System.Collections.IList.this[int index]
    {
        get
        {
            return this[index];
        }

        set
        {
            this[index] = (T)value;
        }
    }

    void System.Collections.ICollection.CopyTo(Array array, int index)
    {
        if (array == null)
            throw new ArgumentNullException(nameof(array), "Destination array cannot be null.");
        CheckRangeArguments(array.Length, index, Count);

        for (int i = 0; i != Count; ++i)
        {
            try
            {
                array.SetValue(this[i], index + i);
            }
            catch (InvalidCastException ex)
            {
                throw new ArgumentException("Destination array is of incorrect type.", ex);
            }
        }
    }

    bool System.Collections.ICollection.IsSynchronized => false;

    object System.Collections.ICollection.SyncRoot => this;

View on GitHub (pinned to 96fad776d2)