jstedfast/MailKit · error · ArgumentNullException
visitor
Error message
visitor
What it means
BodyPartText.Accept(BodyPartVisitor) throws ArgumentNullException when the visitor parameter is null. The method immediately delegates to visitor.VisitBodyPartText, so a null visitor is rejected up front with ArgumentNullException per .NET conventions. A visitor is mandatory for the visitor-pattern traversal of text body parts.
Solutions
- Pass a properly initialized BodyPartVisitor instance.
- Guard the call: only invoke Accept when the visitor is non-null.
- Initialize the visitor eagerly at the start of the traversal routine rather than lazily.
Example fix
// before BodyPartVisitor visitor = null; if (fetchText) visitor = new MyVisitor(); part.Accept(visitor); // after var visitor = new MyVisitor(); // always create, control behavior with flags inside part.Accept(visitor);
Defensive patterns
Strategy: validation
Validate before calling
if (visitor is null)
visitor = new DefaultBodyPartVisitor();
textPart.Accept(visitor); Type guard
bool CanAcceptText(BodyPart part, BodyPartVisitor? visitor) => part is BodyPartText && visitor is not null;
Try / catch
try {
part.Accept(visitor);
} catch (ArgumentNullException ex) when (ex.ParamName == "visitor") {
// visitor was null; create one and retry or skip
part.Accept(new DefaultBodyPartVisitor());
} Prevention
- Initialize visitors before entering any BodyPart.Accept loop.
- Keep visitor creation and Accept calls in the same scope so null states are visible.
- Use ArgumentNullException.ThrowIfNull(visitor) in your own wrapper APIs for fail-fast behavior.
When it happens
Trigger: Calling textBodyPart.Accept(null), commonly when the visitor is built conditionally (e.g. only created when downloading text bodies) and Accept is invoked unconditionally afterwards.
Common situations: En MimeMessage.BodyParts iteration where a developer's visitor variable is set inside an if-block but Accept is called outside it; or after refactoring removed the visitor initialization.
Related errors
AI-assisted analysis of jstedfast/MailKit@9d3859a785 (2026-09-15).
Data as JSON: /api/errors/044e092079b3d65c.
Report an issue: GitHub.
Appendix: source
Thrown at MailKit/BodyPartText.cs:123
/// <summary>
/// Dispatches to the specific visit method for this MIME body part.
/// </summary>
/// <remarks>
/// This default implementation for <see cref="MailKit.BodyPart"/> nodes
/// calls <see cref="MailKit.BodyPartVisitor.VisitBodyPart"/>. Override this
/// method to call into a more specific method on a derived visitor class
/// of the <see cref="MailKit.BodyPartVisitor"/> class. However, it should still
/// support unknown visitors by calling
/// <see cref="MailKit.BodyPartVisitor.VisitBodyPart"/>.
/// </remarks>
/// <param name="visitor">The visitor.</param>
/// <exception cref="System.ArgumentNullException">
/// <paramref name="visitor"/> is <see langword="null" />.
/// </exception>
public override void Accept (BodyPartVisitor visitor)
{
if (visitor == null)
throw new ArgumentNullException (nameof (visitor));
visitor.VisitBodyPartText (this);
}
/// <summary>
/// Encodes the <see cref="BodyPart"/> into the <see cref="System.Text.StringBuilder"/>.
/// </summary>
/// <remarks>
/// Encodes the <see cref="BodyPart"/> into the <see cref="System.Text.StringBuilder"/>.
/// </remarks>
/// <param name="builder">The string builder.</param>
protected override void Encode (StringBuilder builder)
{
base.Encode (builder);
builder.Append (' ');
Encode (builder, Lines);
}View on GitHub (pinned to 9d3859a785)