microsoft/autogen · error · ArgumentNullException

Value cannot be null. (Parameter 'array')

Error message

Value cannot be null. (Parameter 'array')

What it means

Standard ICollection<AIContent>.CopyTo contract enforcement in MultiModalMessage/MultiModalData content collection. CopyTo throws ArgumentNullException(nameof(array)) when the destination array is null, before any bounds checks run.

Solutions

  1. Allocate the destination array with at least Content.Count elements before calling CopyTo
  2. Null-check the array at the call site and fail with your own context-specific error
  3. Prefer ToArray()/ToArray-then-Array.Copy or LINQ CopyTo alternatives that handle allocation for you

Example fix

// before
AIContent[] dest = GetBuffer(); // may return null
msg.Content.CopyTo(dest, 0);
// after
AIContent[] dest = new AIContent[msg.Content.Count];
msg.Content.CopyTo(dest, 0);
Defensive patterns

Strategy: validation

Validate before calling

ArgumentNullException.ThrowIfNull(array);
msg.Content.CopyTo(array, index);

Prevention

When it happens

Trigger: Calling CopyTo(null, index) on the Content collection of a MultiModalMessage, or a generic helper/BCL API (e.g. LINQ buffer copies, collection initializers) invoking CopyTo with a null array.

Common situations: Hand-written CopyTo calls during refactors; generic collection utilities that forward a caller-supplied array without null checks; interop layers that allocate arrays conditionally and pass null on a failed allocation path.

Related errors


AI-assisted analysis of microsoft/autogen@027ecf0a37 (2026-08-15). Data as JSON: /api/errors/89fe321f7187a30a. Report an issue: GitHub.

Appendix: source

Thrown at dotnet/src/Microsoft.AutoGen/AgentChat/Abstractions/Messages.cs:321

    /// <inheritdoc cref="ICollection{AIContent}.Clear" />
    public void Clear()
    {
        this.Content.Clear();
    }

    /// <inheritdoc cref="ICollection{AIContent}.Contains" />
    public bool Contains(AIContent item)
    {
        return this.Content.Any(x => x.AIContent == item);
    }

    /// <inheritdoc cref="ICollection{AIContent}.CopyTo" />
    public void CopyTo(AIContent[] array, int arrayIndex)
    {
        if (array == null)
        {
            throw new ArgumentNullException(nameof(array));
        }

        if (arrayIndex < 0 || arrayIndex >= array.Length)
        {
            throw new ArgumentOutOfRangeException(nameof(arrayIndex));
        }

        if (array.Length - arrayIndex < this.Content.Count)
        {
            throw new ArgumentException("The number of elements in the source is greater than the available space from arrayIndex to the end of the destination array.");
        }

        for (var i = 0; i < this.Content.Count; i++)
        {
            array[arrayIndex + i] = this.Content[i].AIContent;
        }
    }

View on GitHub (pinned to 027ecf0a37)