jstedfast/MailKit · error · ArgumentNullException

Value cannot be null. (Parameter 'array')

Error message

Value cannot be null. (Parameter 'array')

What it means

AccessRights.CopyTo(AccessRight[] array, int arrayIndex) implements ICollection copy semantics: a null destination array throws ArgumentNullException(nameof(array)) at MailKit/AccessRights.cs:227. The array is required as the destination buffer for the rights list.

Solutions

  1. Allocate the destination array first, e.g. var arr = new AccessRight[accessRights.Count]; accessRights.CopyTo(arr, 0).
  2. Alternatively use the collection initializer or LINQ ToArray() instead of calling CopyTo directly.
  3. Add a null check before calling CopyTo.

Example fix

// before
AccessRight[] arr = null;
accessRights.CopyTo(arr, 0);

// after
var arr = new AccessRight[accessRights.Count];
accessRights.CopyTo(arr, 0);
Defensive patterns

Strategy: validation

Validate before calling

var arr = new AccessRight[accessRights.Count];
accessRights.CopyTo(arr, 0);

Type guard

bool CanCopyTo(AccessRight[] arr) => arr != null;

Try / catch

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

Prevention

When it happens

Trigger: Calling accessRights.CopyTo(null, 0) — passing a null array, commonly from a method that returns an array lazily or a variable initialized to null.

Common situations: Serializing rights or converting an AccessRights collection to an array where the destination array construction was skipped or conditional.

Related errors


AI-assisted analysis of jstedfast/MailKit@9d3859a785 (2026-09-15). Data as JSON: /api/errors/2b75f97fd2f5997e. Report an issue: GitHub.

Appendix: source

Thrown at MailKit/AccessRights.cs:227

		/// <summary>
		/// Copies all of the access rights to the specified array.
		/// </summary>
		/// <remarks>
		/// Copies all of the access rights into the array,
		/// starting at the specified array index.
		/// </remarks>
		/// <param name="array">The array.</param>
		/// <param name="arrayIndex">The array index.</param>
		/// <exception cref="System.ArgumentNullException">
		/// <paramref name="array"/> is <see langword="null" />.
		/// </exception>
		/// <exception cref="System.ArgumentOutOfRangeException">
		/// <paramref name="arrayIndex"/> is out of range.
		/// </exception>
		public void CopyTo (AccessRight[] array, int arrayIndex)
		{
			if (array == null)
				throw new ArgumentNullException (nameof (array));

			if (arrayIndex < 0 || arrayIndex + Count > array.Length)
				throw new ArgumentOutOfRangeException (nameof (arrayIndex));

			list.CopyTo (array, arrayIndex);
		}

		/// <summary>
		/// Removes the specified access right.
		/// </summary>
		/// <remarks>
		/// Removes the specified access right.
		/// </remarks>
		/// <returns><see langword="true" /> if the access right was removed; otherwise, <see langword="false" />.</returns>
		/// <param name="right">The access right.</param>
		public bool Remove (AccessRight right)
		{
			return list.Remove (right);

View on GitHub (pinned to 9d3859a785)