{"record":{"id":"0f98eb2f7d78c120","repo":"jstedfast/MailKit","slug":"argumentnullexception-headerset","errorCode":null,"errorMessage":"ArgumentNullException","messagePattern":"ArgumentNullException","errorType":"validation","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"MailKit/HeaderSet.cs","lineNumber":227,"sourceCode":"\n\t\t/// <summary>\n\t\t/// Add the specified header.\n\t\t/// </summary>\n\t\t/// <remarks>\n\t\t/// Adds the specified header to the set of headers.\n\t\t/// </remarks>\n\t\t/// <returns><see langword=\"true\" /> if the header was added to the set; otherwise, <see langword=\"false\" />.</returns>\n\t\t/// <param name=\"header\">The header to add.</param>\n\t\t/// <exception cref=\"ArgumentNullException\">\n\t\t/// <paramref name=\"header\"/> is <see langword=\"null\" />.\n\t\t/// </exception>\n\t\t/// <exception cref=\"InvalidOperationException\">\n\t\t/// The operation is invalid because the <see cref=\"HeaderSet\"/> is read-only.\n\t\t/// </exception>\n\t\tpublic bool Add (string header)\n\t\t{\n\t\t\tif (header == null)\n\t\t\t\tthrow new ArgumentNullException (nameof (header));\n\n\t\t\tif (!IsValid (header))\n\t\t\t\tthrow new ArgumentException (\"The header field is invalid.\", nameof (header));\n\n\t\t\tCheckReadOnly ();\n\n\t\t\treturn hash.Add (header.ToUpperInvariant ());\n\t\t}\n\n\t\t/// <summary>\n\t\t/// Add the specified header.\n\t\t/// </summary>\n\t\t/// <remarks>\n\t\t/// Adds the specified header to the set of headers.\n\t\t/// </remarks>\n\t\t/// <param name=\"item\">The header to add.</param>\n\t\t/// <exception cref=\"ArgumentNullException\">\n\t\t/// <paramref name=\"item\"/> is <see langword=\"null\" />.","sourceCodeStart":209,"sourceCodeEnd":245,"githubUrl":"https://github.com/jstedfast/MailKit/blob/9d3859a7855e3e17582c07fd01972b8e262bf176/MailKit/HeaderSet.cs#L209-L245","documentation":"Guard in HeaderSet.Add: the 'header' argument was null. Adding a null Header to the set is invalid (and would corrupt the set contents), so it is rejected with ArgumentNullException; a read-only set would instead raise InvalidOperationException.","triggerScenarios":"Calling Add(null), often when the header name came from a dictionary lookup, config value, or variable that was never initialized.","commonSituations":"Building fetch/query header lists from dynamic config where an expected key is missing; passing results of string splitting that yielded null.","solutions":["Null-check the header name before calling Add","Use string.IsNullOrWhiteSpace guards when building the list","Skip null entries instead of adding them"],"exampleFix":"// before\nset.Add (headers[\"Custom\"]); // may be null\n// after\nif (headers.TryGetValue (\"Custom\", out var name) && name != null)\n    set.Add (name);","handlingStrategy":"validation","validationCode":"if (!string.IsNullOrEmpty (header))\n    set.Add (header);","typeGuard":"static bool IsValidHeaderInput (string s) => !string.IsNullOrWhiteSpace (s);","tryCatchPattern":"try {\n    set.Add (header);\n} catch (ArgumentNullException ex) {\n    logger.LogWarning (\"Skipped null header name: {Param}\", ex.ParamName);\n}","preventionTips":["Null-check names sourced from dictionaries or config","Use TryGetValue patterns instead of direct indexer access","Filter nulls out of collections before adding to a HeaderSet"],"tags":["csharp","mailkit","argument-null"],"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"}