stride3d/stride · error · ArgumentNullException

Array is null

Error message

Array is null

What it means

Deque<T>'s explicit ICollection<T>.CopyTo(T[] array, int arrayIndex) implementation copies the deque's elements into a strongly-typed array. It begins with argument validation and throws ArgumentNullException('Array is null') when the destination array is null, before any element is copied or range checks are performed.

Solutions

  1. Allocate the destination array (new T[deque.Count]) before calling CopyTo.
  2. Use LINQ Enumerable.ToArray(deque) when a fresh snapshot array is acceptable.
  3. Guard with a null check before the call and handle the empty case.

Example fix

// before
T[] buffer = GetBufferMaybeNull();
deque.CopyTo(buffer, 0); // throws when buffer is null

// after
T[] buffer = GetBufferMaybeNull() ?? new T[deque.Count];
deque.CopyTo(buffer, 0);
Defensive patterns

Strategy: type-guard

Validate before calling

if (destination == null)
    destination = new T[deque.Count];
deque.CopyTo(destination, 0);

Type guard

static bool IsValidDestination<T>(T[]? array, int arrayIndex) =>
    array is not null && arrayIndex >= 0 && arrayIndex <= array.Length;

Try / catch

try
{
    deque.CopyTo(destination, 0);
}
catch (ArgumentNullException ex) when (ex.ParamName == "array")
{
    destination = new T[deque.Count];
    deque.CopyTo(destination, 0);
}

Prevention

When it happens

Trigger: Calling deque.CopyTo(null, 0); passing an array field/property that was never initialized (null); forwarding the result of a method that returned null as the destination array.

Common situations: Interop with APIs that supply the buffer lazily; deserialization stubs with uninitialized target buffers; refactors where array allocation moved but the CopyTo call did not.

Related errors


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

Appendix: source

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

    /// Copies the elements of this list to an <see cref="T:System.Array"/>, starting at a particular <see cref="T:System.Array"/> index.
    /// </summary>
    /// <param name="array">The one-dimensional <see cref="T:System.Array"/> that is the destination of the elements copied from this slice. The <see cref="T:System.Array"/> must have zero-based indexing.</param>
    /// <param name="arrayIndex">The zero-based index in <paramref name="array"/> at which copying begins.</param>
    /// <exception cref="T:System.ArgumentNullException">
    /// <paramref name="array"/> is null.
    /// </exception>
    /// <exception cref="T:System.ArgumentOutOfRangeException">
    /// <paramref name="arrayIndex"/> is less than 0.
    /// </exception>
    /// <exception cref="T:System.ArgumentException">
    /// <paramref name="arrayIndex"/> is equal to or greater than the length of <paramref name="array"/>.
    /// -or-
    /// The number of elements in the source <see cref="T:System.Collections.Generic.ICollection`1"/> is greater than the available space from <paramref name="arrayIndex"/> to the end of the destination <paramref name="array"/>.
    /// </exception>
    void ICollection<T>.CopyTo(T[] array, int arrayIndex)
    {
        if (array == null)
            throw new ArgumentNullException(nameof(array), "Array is null");

        int count = Count;
        CheckRangeArguments(array.Length, arrayIndex, count);
        for (int i = 0; i != count; ++i)
        {
            array[arrayIndex + i] = this[i];
        }
    }

    /// <summary>
    /// Removes the first occurrence of a specific object from this list.
    /// </summary>
    /// <param name="item">The object to remove from this list.</param>
    /// <returns>
    /// true if <paramref name="item"/> was successfully removed from this list; otherwise, false. This method also returns false if <paramref name="item"/> is not found in this list.
    /// </returns>
    /// <exception cref="T:System.NotSupportedException">
    /// This list is read-only.

View on GitHub (pinned to 96fad776d2)