jstedfast/MailKit · error · ArgumentNullException

array

Error message

array

What it means

BodyPartCollection.CopyTo throws ArgumentNullException when the destination BodyPart[] array is null. The collection cannot copy elements into a nonexistent array, so the null array is rejected immediately with the parameter name 'array'.

Solutions

  1. Allocate the destination array before calling CopyTo: var array = new BodyPart[bodyParts.Count];
  2. Guard with if (array != null) bodyParts.CopyTo(array, 0) when the array comes from an optional source.
  3. Use ToArray()/LINQ or foreach enumeration instead of manual CopyTo when you just need a snapshot.

Example fix

// before
BodyPart[] array = null;
bodyParts.CopyTo(array, 0);
// after
BodyPart[] array = new BodyPart[bodyParts.Count];
bodyParts.CopyTo(array, 0);
Defensive patterns

Strategy: validation

Validate before calling

if (array == null) throw new ArgumentException("Destination array must not be null", nameof(array));

Type guard

bool CanCopyTo(BodyPartCollection src, BodyPart[] array, int idx) => array != null && idx >= 0 && idx + src.Count <= array.Length;

Try / catch

try { bodyParts.CopyTo(array, 0); } catch (ArgumentNullException ex) { /* handle missing destination */ }

Prevention

When it happens

Trigger: Calling CopyTo(null, 0) or passing an uninitialized BodyPart[] variable (e.g. a field never assigned) into BodyPartCollection.CopyTo(array, arrayIndex).

Common situations: Test suites (TestBodyPartCollection) exercising null-array behavior; refactoring code where the array allocation was removed or moved after the CopyTo call; copying body parts into a buffer sized later but passed before assignment.

Related errors


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

Appendix: source

Thrown at MailKit/BodyPartCollection.cs:143

		/// <summary>
		/// Copies all of the body parts in the collection to the specified array.
		/// </summary>
		/// <remarks>
		/// Copies all of the body parts within the collection 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 (BodyPart[] array, int arrayIndex)
		{
			if (array == null)
				throw new ArgumentNullException (nameof (array));

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

			collection.CopyTo (array, arrayIndex);
		}

		/// <summary>
		/// Removes the specified body part.
		/// </summary>
		/// <remarks>
		/// Removes the specified body part.
		/// </remarks>
		/// <returns><see langword="true" /> if the body part was removed; otherwise, <see langword="false" />.</returns>
		/// <param name="part">The body part.</param>
		/// <exception cref="System.ArgumentNullException">
		/// <paramref name="part"/> is <see langword="null" />.
		/// </exception>

View on GitHub (pinned to 9d3859a785)