{"record":{"id":"f783a33463fe6784","repo":"jstedfast/MailKit","slug":"visitor-bodypartmessage","errorCode":null,"errorMessage":"visitor","messagePattern":"visitor","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"MailKit/BodyPartMessage.cs","lineNumber":120,"sourceCode":"\t\t/// <summary>\n\t\t/// Dispatches to the specific visit method for this MIME body part.\n\t\t/// </summary>\n\t\t/// <remarks>\n\t\t/// This default implementation for <see cref=\"MailKit.BodyPart\"/> nodes\n\t\t/// calls <see cref=\"MailKit.BodyPartVisitor.VisitBodyPart\"/>. Override this\n\t\t/// method to call into a more specific method on a derived visitor class\n\t\t/// of the <see cref=\"MailKit.BodyPartVisitor\"/> class. However, it should still\n\t\t/// support unknown visitors by calling\n\t\t/// <see cref=\"MailKit.BodyPartVisitor.VisitBodyPart\"/>.\n\t\t/// </remarks>\n\t\t/// <param name=\"visitor\">The visitor.</param>\n\t\t/// <exception cref=\"System.ArgumentNullException\">\n\t\t/// <paramref name=\"visitor\"/> is <see langword=\"null\" />.\n\t\t/// </exception>\n\t\tpublic override void Accept (BodyPartVisitor visitor)\n\t\t{\n\t\t\tif (visitor == null)\n\t\t\t\tthrow new ArgumentNullException (nameof (visitor));\n\n\t\t\tvisitor.VisitBodyPartMessage (this);\n\t\t}\n\n\t\t/// <summary>\n\t\t/// Encodes the <see cref=\"BodyPart\"/> into the <see cref=\"System.Text.StringBuilder\"/>.\n\t\t/// </summary>\n\t\t/// <remarks>\n\t\t/// Encodes the <see cref=\"BodyPart\"/> into the <see cref=\"System.Text.StringBuilder\"/>.\n\t\t/// </remarks>\n\t\t/// <param name=\"builder\">The string builder.</param>\n\t\tprotected override void Encode (StringBuilder builder)\n\t\t{\n\t\t\tbase.Encode (builder);\n\n\t\t\tbuilder.Append (' ');\n\t\t\tEncode (builder, Envelope);\n\t\t\tbuilder.Append (' ');","sourceCodeStart":102,"sourceCodeEnd":138,"githubUrl":"https://github.com/jstedfast/MailKit/blob/9d3859a7855e3e17582c07fd01972b8e262bf176/MailKit/BodyPartMessage.cs#L102-L138","documentation":"BodyPartMessage.Accept(BodyPartVisitor) throws ArgumentNullException when the visitor parameter is null. The visitor-pattern dispatch delegates directly to visitor.VisitBodyPartMessage, so a null visitor would cause a NullReferenceException later; the library validates up front. A non-null visitor is required to traverse this message body part.","triggerScenarios":"Calling bodyPart.Accept(null) on a BodyPartMessage, typically when the visitor was conditionally created (e.g. null returned from a factory or not yet initialized).","commonSituations":"Constructing a visitor based on options (like a BodyPartVisitor subclass for HTML/text extraction) where a config branch leaves the variable null, then unconditionally calling Accept during body-structure traversal.","solutions":["Pass a valid BodyPartVisitor instance to Accept.","If a visitor is optional in your logic, guard with an if (visitor != null) check before calling Accept.","Fix the visitor construction path so it never yields null (throw at construction time with a clear message instead)."],"exampleFix":"// before\npart.Accept(visitor); // visitor may be null\n// after\nif (visitor != null)\n    part.Accept(visitor);","handlingStrategy":"validation","validationCode":"if (visitor is null)\n    throw new InvalidOperationException(\"No visitor configured for body structure traversal.\");\nmessageBodyPart.Accept(visitor);","typeGuard":"bool CanAccept(BodyPart part, BodyPartVisitor? visitor) => part is BodyPartMessage && visitor is not null;","tryCatchPattern":"try {\n    part.Accept(visitor);\n} catch (ArgumentNullException ex) when (ex.ParamName == \"visitor\") {\n    logger.LogWarning(\"Traversal skipped: visitor was null\");\n}","preventionTips":["Create the visitor eagerly at the start of the traversal method.","Validate visitor at your own public API boundary with ArgumentNullException.ThrowIfNull.","Avoid factories that can return null visitors; return a no-op visitor instead."],"tags":["null-reference","visitor-pattern","argument-validation","mailkit"],"backgroundTag":"null-argument","analyzedSha":"9d3859a7855e3e17582c07fd01972b8e262bf176","analyzedAt":"2026-09-15T15:46:11.592Z","contentChangedAt":"2026-09-15T15:46:11.592Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}