jstedfast/MailKit · error · ArgumentNullException

Value cannot be null. (Parameter 'labels')

Error message

Value cannot be null. (Parameter 'labels')

What it means

The MessageLabelsChangedEventArgs(int index, IList<string> labels) constructor requires a non-null labels list. ArgumentNullException is thrown when labels is null; an empty list is the correct way to express 'no labels'.

Solutions

  1. Pass an empty List<string> instead of null
  2. Coalesce null label collections to an empty list before constructing the args
  3. Fix the producer that supplies the labels so it always returns a list

Example fix

// before
new MessageLabelsChangedEventArgs(index, null);
// after
new MessageLabelsChangedEventArgs(index, labels ?? new List<string>());
Defensive patterns

Strategy: validation

Validate before calling

if (labels == null) labels = new List<string>();
var args = new MessageLabelsChangedEventArgs(index, labels);

Type guard

IList<string> NonNull(IList<string> list) => list ?? new List<string>();

Try / catch

try { args = new MessageLabelsChangedEventArgs(index, labels); }
catch (ArgumentNullException ex) when (ex.ParamName == "labels") { args = new MessageLabelsChangedEventArgs(index, new List<string>()); }

Prevention

When it happens

Trigger: Constructing MessageLabelsChangedEventArgs with labels = null, e.g. in custom Gmail-capable folder code raising labels-changed events.

Common situations: Custom IMailFolder implementations or X-GM-LABELS event plumbing where the label list was never initialized before event emission.

Related errors


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

Appendix: source

Thrown at MailKit/MessageLabelsChangedEventArgs.cs:57

	{
		/// <summary>
		/// Initializes a new instance of the <see cref="MailKit.MessageLabelsChangedEventArgs"/> class.
		/// </summary>
		/// <remarks>
		/// Creates a new <see cref="MessageLabelsChangedEventArgs"/>.
		/// </remarks>
		/// <param name="index">The message index.</param>
		/// <param name="labels">The message labels.</param>
		/// <exception cref="System.ArgumentNullException">
		/// <paramref name="labels"/> is <see langword="null" />.
		/// </exception>
		/// <exception cref="System.ArgumentOutOfRangeException">
		/// <paramref name="index"/> is out of range.
		/// </exception>
		public MessageLabelsChangedEventArgs (int index, IList<string> labels) : base (index)
		{
			if (labels == null)
				throw new ArgumentNullException (nameof (labels));

			Labels = new ReadOnlyCollection<string> (labels);
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="MailKit.MessageLabelsChangedEventArgs"/> class.
		/// </summary>
		/// <remarks>
		/// Creates a new <see cref="MessageLabelsChangedEventArgs"/>.
		/// </remarks>
		/// <param name="index">The message index.</param>
		/// <param name="labels">The message labels.</param>
		/// <param name="modseq">The modification sequence value.</param>
		/// <exception cref="System.ArgumentNullException">
		/// <paramref name="labels"/> is <see langword="null" />.
		/// </exception>
		/// <exception cref="System.ArgumentOutOfRangeException">
		/// <paramref name="index"/> is out of range.

View on GitHub (pinned to 9d3859a785)