{"record":{"id":"e9e503302dc7979f","repo":"jstedfast/MailKit","slug":"cannot-search-for-null-or-empty-keywords","errorCode":null,"errorMessage":"Cannot search for null or empty keywords.","messagePattern":"Cannot search for null or empty keywords\\.","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"MailKit/Search/SearchQuery.cs","lineNumber":544,"sourceCode":"\t\t/// <param name=\"keywords\">The keywords.</param>\n\t\t/// <exception cref=\"System.ArgumentNullException\">\n\t\t/// <paramref name=\"keywords\"/> is <see langword=\"null\" />.\n\t\t/// </exception>\n\t\t/// <exception cref=\"System.ArgumentException\">\n\t\t/// <para>One or more of the <paramref name=\"keywords\"/> is <see langword=\"null\" /> or empty.</para>\n\t\t/// <para>-or-</para>\n\t\t/// <para>No keywords were given.</para>\n\t\t/// </exception>\n\t\tpublic static SearchQuery HasKeywords (IEnumerable<string> keywords)\n\t\t{\n\t\t\tif (keywords == null)\n\t\t\t\tthrow new ArgumentNullException (nameof (keywords));\n\n\t\t\tvar list = new List<SearchQuery> ();\n\n\t\t\tforeach (var keyword in keywords) {\n\t\t\t\tif (string.IsNullOrEmpty (keyword))\n\t\t\t\t\tthrow new ArgumentException (\"Cannot search for null or empty keywords.\", nameof (keywords));\n\n\t\t\t\tlist.Add (new TextSearchQuery (SearchTerm.Keyword, keyword));\n\t\t\t}\n\n\t\t\tif (list.Count == 0)\n\t\t\t\tthrow new ArgumentException (\"No keywords specified.\", nameof (keywords));\n\n\t\t\tvar query = list[0];\n\t\t\tfor (int i = 1; i < list.Count; i++)\n\t\t\t\tquery = query.And (list[i]);\n\n\t\t\treturn query;\n\t\t}\n\n\t\t/// <summary>\n\t\t/// Match messages that do not have the specified keyword set.\n\t\t/// </summary>\n\t\t/// <remarks>","sourceCodeStart":526,"sourceCodeEnd":562,"githubUrl":"https://github.com/jstedfast/MailKit/blob/9d3859a7855e3e17582c07fd01972b8e262bf176/MailKit/Search/SearchQuery.cs#L526-L562","documentation":"While enumerating, HasKeywords throws ArgumentException if any individual keyword is null or empty, because each becomes an IMAP KEYWORD atom that must be non-empty. The message is 'Cannot search for null or empty keywords.'","triggerScenarios":"Calling SearchQuery.HasKeywords with a collection containing \"\" or null elements, e.g. from splitting a string: \"a,,b\".Split(',') or user-entered tags not filtered.","commonSituations":"Splitting a comma-separated input string that contains consecutive separators or trailing commas; tag lists from a database with blank rows; partially initialized deserialized arrays with null entries.","solutions":["Filter the collection first: `keywords.Where(k => !string.IsNullOrEmpty(k))` before calling HasKeywords.","Trim and split defensively, e.g. `input.Split(',', StringSplitOptions.RemoveEmptyEntries).Select(k => k.Trim())`.","Validate each element in your own loop and report which index/keyword is bad for a better user error."],"exampleFix":"// before\nvar query = SearchQuery.HasKeywords(input.Split(','));\n// after\nvar kws = input.Split(',', StringSplitOptions.RemoveEmptyEntries)\n    .Select(k => k.Trim())\n    .Where(k => k.Length > 0)\n    .ToList();\nvar query = SearchQuery.HasKeywords(kws);","handlingStrategy":"validation","validationCode":"var clean = keywords?.Where(k => !string.IsNullOrEmpty(k)).ToList();\nif (clean == null || clean.Count == 0)\n    return SearchQuery.All;\nvar query = SearchQuery.HasKeywords(clean);","typeGuard":"bool AllKeywordsValid(IEnumerable<string> kws) => kws != null && kws.All(k => !string.IsNullOrEmpty(k));","tryCatchPattern":"try { query = SearchQuery.HasKeywords(keywords); }\ncatch (ArgumentException ex) { log.Warn(ex.Message); query = SearchQuery.All; }","preventionTips":["Use StringSplitOptions.RemoveEmptyEntries when splitting input strings into keywords.","Sanitize every element (null/empty filter) before passing collections to MailKit search.","Normalize user tag input (trim, drop blanks) in one shared helper."],"tags":["argument-validation","empty-string","collection","mailkit"],"backgroundTag":"empty-required-field","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"}