jstedfast/MailKit · error · ArgumentNullException

Value cannot be null. (Parameter 'messages')

Error message

Value cannot be null. (Parameter 'messages')

What it means

MessageSorter.Sort throws ArgumentNullException when the `orderBy` sort-order list is null. The Sort extension method needs at least one OrderBy criterion to define how the IMessageSummary list should be ordered; a null list provides no sort specification at all.

Solutions

  1. Pass a non-empty list of OrderBy criteria, e.g. `new List<OrderBy> { OrderBy.Date }`.
  2. Coalesce to a default: `orderBy ?? new List<OrderBy> { OrderBy.Date }`.
  3. Initialize orderBy from settings with a fallback default before calling Sort.

Example fix

// before
var sorted = messages.Sort(orderBy); // orderBy == null
// after
var sorted = messages.Sort(orderBy ?? new List<OrderBy> { OrderBy.Date });
Defensive patterns

Strategy: validation

Validate before calling

if (orderBy == null || orderBy.Count == 0)
    orderBy = new List<OrderBy> { OrderBy.Date };
var sorted = messages.Sort(orderBy);

Type guard

bool IsValidOrderBy(IList<OrderBy> orderBy) => orderBy != null && orderBy.Count > 0;

Try / catch

try {
    return messages.Sort(orderBy);
} catch (ArgumentNullException ex) when (ex.ParamName == "orderBy") {
    return messages.Sort(new List<OrderBy> { OrderBy.Date });
}

Prevention

When it happens

Trigger: Calling `messages.Sort(null)` on an IEnumerable<IMessageSummary>, or passing an orderBy variable that was never initialized / that a config loader deserialized as null.

Common situations: Building sort orders dynamically from user preferences where the preference key is missing and the resulting list is null; refactoring that removed the default sort-order initialization; null-conditional chains like `GetSortOrder()?.OrderBy` evaluated to null.

Related errors


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

Appendix: source

Thrown at MailKit/MessageSorter.cs:221

		/// </remarks>
		/// <returns>The sorted messages.</returns>
		/// <typeparam name="T">The message items must implement the <see cref="IMessageSummary"/> interface.</typeparam>
		/// <param name="messages">The messages to sort.</param>
		/// <param name="orderBy">The sort ordering.</param>
		/// <exception cref="System.ArgumentNullException">
		/// <para><paramref name="messages"/> is <see langword="null" />.</para>
		/// <para>-or-</para>
		/// <para><paramref name="orderBy"/> is <see langword="null" />.</para>
		/// </exception>
		/// <exception cref="System.ArgumentException">
		/// <para><paramref name="messages"/> contains one or more items that is missing information needed for sorting.</para>
		/// <para>-or-</para>
		/// <para><paramref name="orderBy"/> is an empty list.</para>
		/// </exception>
		public static IList<T> Sort<T> (this IEnumerable<T> messages, IList<OrderBy> orderBy) where T : IMessageSummary
		{
			if (messages == null)
				throw new ArgumentNullException (nameof (messages));

			if (orderBy == null)
				throw new ArgumentNullException (nameof (orderBy));

			if (orderBy.Count == 0)
				throw new ArgumentException ("No sort order provided.", nameof (orderBy));

			var requiredFields = GetMessageSummaryItems (orderBy);
			var list = new List<T> ();

			foreach (var message in messages) {
				if ((message.Fields & requiredFields) != requiredFields)
					throw new ArgumentException ("One or more messages is missing information needed for sorting.", nameof (messages));

				list.Add (message);
			}

			if (list.Count < 2)

View on GitHub (pinned to 9d3859a785)