jstedfast/MailKit · error · ArgumentNullException
Value cannot be null. (Parameter 'partSpecifier')
Error message
Value cannot be null. (Parameter 'partSpecifier')
What it means
AnnotationEntry.ValidatePartSpecifier throws ArgumentNullException when the partSpecifier argument is null. The part specifier identifies the MIME part (e.g. "1.2" or "text") the annotation applies to; a null value cannot form a valid entry.
Solutions
- Pass a valid non-null part specifier string (e.g. "1" or "text")
- If the annotation is for the whole message, use the constructor overload without a part specifier instead of passing null
- Null-check or coalesce the value before calling: partSpecifier ?? ""
Example fix
// before
var entry = AnnotationEntry.Create (null, "comment", AnnotationScope.Both);
// after
var entry = AnnotationEntry.Create ("1", "comment", AnnotationScope.Both); Defensive patterns
Strategy: validation
Validate before calling
if (string.IsNullOrEmpty (partSpecifier))
throw new ArgumentException ("Part specifier is required (e.g. \"1\" or \"1.2\")."); Type guard
bool IsValidPartSpecifier (string? s) =>
!string.IsNullOrEmpty (s) && s.All (c => char.IsDigit (c) || c == '.')
&& !s.StartsWith (".") && !s.EndsWith (".") && !s.Contains (".."); Try / catch
try {
var entry = AnnotationEntry.Create (partSpecifier, path, scope);
} catch (ArgumentNullException ex) when (ex.ParamName == "partSpecifier") {
logger.LogError (ex, "Part specifier was null");
} Prevention
- Null-check values derived from BodyPart.PartSpecifier before use
- Use the constructor overload matching the data you actually have
- Prefer compiling the part specifier from known integers rather than parsing strings
When it happens
Trigger: Passing a null partSpecifier to an AnnotationEntry constructor/factory overload that takes one, typically because a BodyPart.PartSpecifier lookup returned null or a variable was never initialized.
Common situations: Deriving the part specifier from parsed message metadata that lacked a value; calling the partSpecifier-based constructor instead of the BodyPart-based one when no part exists.
Related errors
- Value cannot be null. (Parameter 'part')
- Value cannot be null. (Parameter 'entry')
- Value cannot be null. (Parameter 'entry')
- Annotation entry paths must not end with '/'.
- Annotation entry paths must not end with '.'.
AI-assisted analysis of jstedfast/MailKit@9d3859a785 (2026-09-15).
Data as JSON: /api/errors/8a3437b32d2985e5.
Report an issue: GitHub.
Appendix: source
Thrown at MailKit/AnnotationEntry.cs:158
if ((pc == '/' || pc == '.') && (c == '/' || c == '.'))
throw new ArgumentException ("Invalid annotation entry path.", nameof (path));
pc = c;
}
int endIndex = path.Length - 1;
if (path[endIndex] == '/')
throw new ArgumentException ("Annotation entry paths must not end with '/'.", nameof (path));
if (path[endIndex] == '.')
throw new ArgumentException ("Annotation entry paths must not end with '.'.", nameof (path));
}
static void ValidatePartSpecifier (string partSpecifier)
{
if (partSpecifier == null)
throw new ArgumentNullException (nameof (partSpecifier));
char pc = '\0';
for (int i = 0; i < partSpecifier.Length; i++) {
char c = partSpecifier[i];
if (!((c >= '0' && c <= '9') || c == '.') || (c == '.' && (pc == '.' || pc == '\0')))
throw new ArgumentException ("Invalid part-specifier.", nameof (partSpecifier));
pc = c;
}
if (pc == '.')
throw new ArgumentException ("Invalid part-specifier.", nameof (partSpecifier));
}
AnnotationEntry (string? partSpecifier, string entry, string path, AnnotationScope scope)
{View on GitHub (pinned to 9d3859a785)