{"record":{"id":"66e251dfe67a73ac","repo":"jstedfast/MailKit","slug":"value-cannot-be-null-parameter-messages","errorCode":null,"errorMessage":"Value cannot be null. (Parameter 'messages')","messagePattern":"Value cannot be null\\. \\(Parameter 'messages'\\)","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"MailKit/MessageSorter.cs","lineNumber":221,"sourceCode":"\t\t/// </remarks>\n\t\t/// <returns>The sorted messages.</returns>\n\t\t/// <typeparam name=\"T\">The message items must implement the <see cref=\"IMessageSummary\"/> interface.</typeparam>\n\t\t/// <param name=\"messages\">The messages to sort.</param>\n\t\t/// <param name=\"orderBy\">The sort ordering.</param>\n\t\t/// <exception cref=\"System.ArgumentNullException\">\n\t\t/// <para><paramref name=\"messages\"/> is <see langword=\"null\" />.</para>\n\t\t/// <para>-or-</para>\n\t\t/// <para><paramref name=\"orderBy\"/> is <see langword=\"null\" />.</para>\n\t\t/// </exception>\n\t\t/// <exception cref=\"System.ArgumentException\">\n\t\t/// <para><paramref name=\"messages\"/> contains one or more items that is missing information needed for sorting.</para>\n\t\t/// <para>-or-</para>\n\t\t/// <para><paramref name=\"orderBy\"/> is an empty list.</para>\n\t\t/// </exception>\n\t\tpublic static IList<T> Sort<T> (this IEnumerable<T> messages, IList<OrderBy> orderBy) where T : IMessageSummary\n\t\t{\n\t\t\tif (messages == null)\n\t\t\t\tthrow new ArgumentNullException (nameof (messages));\n\n\t\t\tif (orderBy == null)\n\t\t\t\tthrow new ArgumentNullException (nameof (orderBy));\n\n\t\t\tif (orderBy.Count == 0)\n\t\t\t\tthrow new ArgumentException (\"No sort order provided.\", nameof (orderBy));\n\n\t\t\tvar requiredFields = GetMessageSummaryItems (orderBy);\n\t\t\tvar list = new List<T> ();\n\n\t\t\tforeach (var message in messages) {\n\t\t\t\tif ((message.Fields & requiredFields) != requiredFields)\n\t\t\t\t\tthrow new ArgumentException (\"One or more messages is missing information needed for sorting.\", nameof (messages));\n\n\t\t\t\tlist.Add (message);\n\t\t\t}\n\n\t\t\tif (list.Count < 2)","sourceCodeStart":203,"sourceCodeEnd":239,"githubUrl":"https://github.com/jstedfast/MailKit/blob/9d3859a7855e3e17582c07fd01972b8e262bf176/MailKit/MessageSorter.cs#L203-L239","documentation":"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.","triggerScenarios":"Calling `messages.Sort(null)` on an IEnumerable<IMessageSummary>, or passing an orderBy variable that was never initialized / that a config loader deserialized as null.","commonSituations":"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.","solutions":["Pass a non-empty list of OrderBy criteria, e.g. `new List<OrderBy> { OrderBy.Date }`.","Coalesce to a default: `orderBy ?? new List<OrderBy> { OrderBy.Date }`.","Initialize orderBy from settings with a fallback default before calling Sort."],"exampleFix":"// before\nvar sorted = messages.Sort(orderBy); // orderBy == null\n// after\nvar sorted = messages.Sort(orderBy ?? new List<OrderBy> { OrderBy.Date });","handlingStrategy":"validation","validationCode":"if (orderBy == null || orderBy.Count == 0)\n    orderBy = new List<OrderBy> { OrderBy.Date };\nvar sorted = messages.Sort(orderBy);","typeGuard":"bool IsValidOrderBy(IList<OrderBy> orderBy) => orderBy != null && orderBy.Count > 0;","tryCatchPattern":"try {\n    return messages.Sort(orderBy);\n} catch (ArgumentNullException ex) when (ex.ParamName == \"orderBy\") {\n    return messages.Sort(new List<OrderBy> { OrderBy.Date });\n}","preventionTips":["Initialize sort-order lists with a default (OrderBy.Date or OrderBy.Arrival).","Avoid `?.` chains when building orderBy; use ?? fallbacks.","Centralize sort-order construction in one helper that never returns null."],"tags":["csharp","mailkit","argument-null","sorting"],"backgroundTag":"null-argument","analyzedSha":"9d3859a7855e3e17582c07fd01972b8e262bf176","analyzedAt":"2026-09-15T15:46:11.592Z","contentChangedAt":"2026-09-15T15:46:11.592Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}