{"record":{"id":"95cd36eb9e441c8c","repo":"jstedfast/MailKit","slug":"value-cannot-be-null-parameter-part","errorCode":null,"errorMessage":"Value cannot be null. (Parameter 'part')","messagePattern":"Value cannot be null\\. \\(Parameter 'part'\\)","errorType":"validation","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"MailKit/AnnotationEntry.cs","lineNumber":267,"sourceCode":"\t\t/// </summary>\n\t\t/// <remarks>\n\t\t/// Creates a new <see cref=\"AnnotationEntry\"/> for an individual body part of a message.\n\t\t/// </remarks>\n\t\t/// <param name=\"part\">The body part of the message.</param>\n\t\t/// <param name=\"path\">The annotation entry path.</param>\n\t\t/// <param name=\"scope\">The scope of the annotation.</param>\n\t\t/// <exception cref=\"System.ArgumentNullException\">\n\t\t/// <para><paramref name=\"part\"/> is <see langword=\"null\" />.</para>\n\t\t/// <para>-or-</para>\n\t\t/// <para><paramref name=\"path\"/> is <see langword=\"null\" />.</para>\n\t\t/// </exception>\n\t\t/// <exception cref=\"System.ArgumentException\">\n\t\t/// <paramref name=\"path\"/> is invalid.\n\t\t/// </exception>\n\t\tpublic AnnotationEntry (BodyPart part, string path, AnnotationScope scope = AnnotationScope.Both)\n\t\t{\n\t\t\tif (part == null)\n\t\t\t\tthrow new ArgumentNullException (nameof (part));\n\n\t\t\tValidatePath (path);\n\n\t\t\tswitch (scope) {\n\t\t\tcase AnnotationScope.Private: Entry = string.Format (\"/{0}{1}.priv\", part.PartSpecifier, path); break;\n\t\t\tcase AnnotationScope.Shared: Entry = string.Format (\"/{0}{1}.shared\", part.PartSpecifier, path); break;\n\t\t\tdefault: Entry = string.Format (\"/{0}{1}\", part.PartSpecifier, path); break;\n\t\t\t}\n\n\t\t\tPartSpecifier = part.PartSpecifier;\n\t\t\tPath = path;\n\t\t\tScope = scope;\n\t\t}\n\n\t\t/// <summary>\n\t\t/// Get the annotation entry specifier.\n\t\t/// </summary>\n\t\t/// <remarks>","sourceCodeStart":249,"sourceCodeEnd":285,"githubUrl":"https://github.com/jstedfast/MailKit/blob/9d3859a7855e3e17582c07fd01972b8e262bf176/MailKit/AnnotationEntry.cs#L249-L285","documentation":"Guard clause in the AnnotationEntry constructor: the mandatory 'part' argument (the message body part the annotation entry is attached to) was null, so no entry can be created without a body part. Callers must pass a valid BodyPart.","triggerScenarios":"Calling new AnnotationEntry(part, path, scope) with a null BodyPart, often from message summarization code where the requested body part was absent from the response (e.g. ImapFolder.GetBodyPart returned null).","commonSituations":"Fetching a body part that does not exist in the message (bad part specifier) and passing the null result straight into the constructor without checking.","solutions":["Ensure the BodyPart is fetched successfully before constructing the entry (null-check the result of GetBodyPart/message.Summarize lookups)","Use the partSpecifier-based overload with a known part number if no BodyPart object is available","Guard: if (part == null) throw/handle before constructing"],"exampleFix":"// before\nvar part = folder.GetBodyPart (uid, \"9.9\");\nvar entry = new AnnotationEntry (part, \"comment\");\n// after\nvar part = folder.GetBodyPart (uid, \"9.9\");\nif (part != null)\n    var entry = new AnnotationEntry (part, \"comment\");","handlingStrategy":"type-guard","validationCode":"if (part == null)\n    throw new InvalidOperationException ($\"Body part '{spec}' was not found in the message.\");","typeGuard":"bool TryGetBodyPart (ImapFolder folder, UniqueId uid, string spec, out BodyPart? part) {\n    part = folder.GetBodyPart (uid, spec) as BodyPart;\n    return part != null;\n}","tryCatchPattern":"try {\n    var entry = new AnnotationEntry (part, path, scope);\n} catch (ArgumentNullException ex) when (ex.ParamName == \"part\") {\n    logger.LogError (ex, \"Cannot annotate: body part is null\");\n}","preventionTips":["Null-check GetBodyPart / Summarize results before use","Verify the part specifier exists in message.BodyParts","Use the partSpecifier overload when no BodyPart object is available"],"tags":["imap","annotation","null-argument"],"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"}