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.
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)
Solutions
- Allocate the destination array with at least Content.Count elements before calling CopyTo
- Null-check the array at the call site and fail with your own context-specific error
- 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
- Always allocate the destination array yourself rather than accepting caller buffers unchecked
- Use ArgumentNullException.ThrowIfNull at API boundaries
- Prefer msg.Content.ToArray() over CopyTo when you just need a snapshot
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
- Specified argument was out of the range of valid values. (Pa
- The number of elements in the source is greater than the ava
- modelName is a required property for LMStudioConfig and cann
- The recent update is not a TextMessage
- The content has more than one choice. Please try another inp
AI-assisted analysis of microsoft/autogen@027ecf0a37 (2026-08-15).
Data as JSON: /api/errors/89fe321f7187a30a.
Report an issue: GitHub.