{"record":{"id":"c76fa749552fa136","repo":"jstedfast/MailKit","slug":"value-cannot-be-null-parameter-uids","errorCode":null,"errorMessage":"Value cannot be null. (Parameter 'uids')","messagePattern":"Value cannot be null\\. \\(Parameter 'uids'\\)","errorType":"validation","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"MailKit/Net/Imap/ImapFolder.cs","lineNumber":4268,"sourceCode":"\t\t/// <exception cref=\"FolderNotOpenException\">\n\t\t/// The <see cref=\"ImapFolder\"/> is not currently open in read-write mode.\n\t\t/// </exception>\n\t\t/// <exception cref=\"System.OperationCanceledException\">\n\t\t/// The operation was canceled via the cancellation token.\n\t\t/// </exception>\n\t\t/// <exception cref=\"System.IO.IOException\">\n\t\t/// An I/O error occurred.\n\t\t/// </exception>\n\t\t/// <exception cref=\"ImapProtocolException\">\n\t\t/// The server's response contained unexpected tokens.\n\t\t/// </exception>\n\t\t/// <exception cref=\"ImapCommandException\">\n\t\t/// The server replied with a NO or BAD response.\n\t\t/// </exception>\n\t\tpublic override void Expunge (IList<UniqueId> uids, CancellationToken cancellationToken = default)\n\t\t{\n\t\t\tif (uids == null)\n\t\t\t\tthrow new ArgumentNullException (nameof (uids));\n\n\t\t\tCheckState (true, true);\n\n\t\t\tif (uids.Count == 0)\n\t\t\t\treturn;\n\n\t\t\tif ((Engine.Capabilities & ImapCapabilities.UidPlus) == 0) {\n\t\t\t\t// get the list of messages marked for deletion that should not be expunged\n\t\t\t\tvar query = SearchQuery.Deleted.And (SearchQuery.Not (SearchQuery.Uids (uids)));\n\t\t\t\tvar unmark = Search (SearchOptions.None, query, cancellationToken);\n\n\t\t\t\tif (unmark.Count > 0) {\n\t\t\t\t\t// clear the \\Deleted flag on all messages except the ones that are to be expunged\n\t\t\t\t\tStore (unmark.UniqueIds, RemoveDeletedFlag, cancellationToken);\n\t\t\t\t}\n\n\t\t\t\t// expunge the folder\n\t\t\t\tExpunge (cancellationToken);","sourceCodeStart":4250,"sourceCodeEnd":4286,"githubUrl":"https://github.com/jstedfast/MailKit/blob/9d3859a7855e3e17582c07fd01972b8e262bf176/MailKit/Net/Imap/ImapFolder.cs#L4250-L4286","documentation":"ImapFolder.Expunge(IList<UniqueId>, ...) validates its uids argument before doing anything; a null list is rejected with ArgumentNullException. Note that an empty (non-null) list is accepted and is a no-op.","triggerScenarios":"Calling Expunge(null) directly, or passing the result of a method/lookup that returned null instead of an empty collection.","commonSituations":"Storing the UID list in a variable that was never initialized; a search/annotation lookup returning null on failure and being fed straight into Expunge.","solutions":["Pass a non-null IList<UniqueId>; pass an empty list (or use the parameterless Expunge()) to expunge nothing.","Guard with ?? new List<UniqueId>() or an early null check at the call site.","If you only want to expunge all flagged-deleted messages, call Expunge() without the uids argument."],"exampleFix":"// before\nfolder.Expunge (uids);\n// after\nfolder.Expunge (uids ?? new List<UniqueId> ());","handlingStrategy":"validation","validationCode":"if (uids == null) throw new InvalidOperationException (\"uids must be provided; pass an empty list to expunge nothing\");","typeGuard":"IList<UniqueId> SafeUids (IList<UniqueId> u) => u ?? new List<UniqueId> ();","tryCatchPattern":"try { folder.Expunge (uids); } catch (ArgumentNullException ex) when (ex.ParamName == \"uids\") { /* supply a valid list */ }","preventionTips":["Never pass raw results of nullable lookups directly into Expunge","Remember empty list is a valid no-op; null is not","Prefer the parameterless Expunge() for unconditional expunge"],"tags":["imap","mailkit","null-argument","expunge"],"backgroundTag":"null-argument","analyzedSha":"9d3859a7855e3e17582c07fd01972b8e262bf176","analyzedAt":"2026-09-15T15:46:11.592Z","contentChangedAt":"2026-09-15T15:46:11.592Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}