jstedfast/MailKit · error · ArgumentNullException

Value cannot be null. (Parameter 'annotations')

Error message

Value cannot be null. (Parameter 'annotations')

What it means

The AnnotationsChangedEventArgs constructor requires a non-null collection of changed annotations. Passing null throws ArgumentNullException naming the 'annotations' parameter, because the event args must wrap a concrete read-only list.

Solutions

  1. Pass an empty collection (e.g. Array.Empty<Annotation>()) instead of null when no annotations changed
  2. Guard the call site with a null check and substitute a default empty list
  3. If annotations are unknown, do not raise the event

Example fix

// before
new AnnotationsChangedEventArgs (0, null);
// after
new AnnotationsChangedEventArgs (0, Array.Empty<Annotation> ());
Defensive patterns

Strategy: type-guard

Validate before calling

if (annotations == null) annotations = Array.Empty<Annotation> ();

Type guard

bool HasAnnotations (IEnumerable<Annotation> annotations) => annotations != null;

Try / catch

try { var args = new AnnotationsChangedEventArgs (index, annotations); }
catch (ArgumentNullException) { args = new AnnotationsChangedEventArgs (index, Array.Empty<Annotation> ()); }

Prevention

When it happens

Trigger: Constructing new AnnotationsChangedEventArgs (index, null) when raising a folder annotations-changed event or in tests.

Common situations: Custom IMailFolder implementations raising the AnnotationsChanged event without annotation data, unit tests passing null for convenience.

Related errors


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

Appendix: source

Thrown at MailKit/AnnotationsChangedEventArgs.cs:55

	/// Event args used when an annotation changes.
	/// </remarks>
	public class AnnotationsChangedEventArgs : MessageEventArgs
	{
		/// <summary>
		/// Initializes a new instance of the <see cref="T:MailKit.AnnotationsChangedEventArgs"/> class.
		/// </summary>
		/// <remarks>
		/// Creates a new <see cref="AnnotationsChangedEventArgs"/>.
		/// </remarks>
		/// <param name="index">The message index.</param>
		/// <param name="annotations">The annotations that changed.</param>
		/// <exception cref="System.ArgumentNullException">
		/// <paramref name="annotations"/> is <see langword="null" />.
		/// </exception>
		public AnnotationsChangedEventArgs (int index, IEnumerable<Annotation> annotations) : base (index)
		{
			if (annotations == null)
				throw new ArgumentNullException (nameof (annotations));

			Annotations = new ReadOnlyCollection<Annotation> (annotations.ToArray ());
		}

		/// <summary>
		/// Get the annotations that changed.
		/// </summary>
		/// <remarks>
		/// Gets the annotations that changed.
		/// </remarks>
		/// <value>The annotation.</value>
		public IList<Annotation> Annotations {
			get; internal set;
		}

		/// <summary>
		/// Gets the updated mod-sequence value of the message, if available.
		/// </summary>

View on GitHub (pinned to 9d3859a785)