jstedfast/MailKit · error · ArgumentNullException
visitor
Error message
visitor
What it means
BodyPartMultipart.Accept(BodyPartVisitor) throws ArgumentNullException when the visitor parameter is null. Accept dispatches to visitor.VisitBodyPartMultipart, which requires a live visitor object; a null argument would crash inside the call, so the method validates immediately. Every concrete BodyPart subtype follows this same pattern.
Solutions
- Ensure a non-null BodyPartVisitor is constructed before traversing.
- Add a null check before calling Accept when the visitor is optional in your flow.
- Validate visitor at your own public API boundary so nulls never reach library calls.
Example fix
// before
foreach (var part in bodyParts)
part.Accept(visitor); // may be null
// after
ArgumentNullException.ThrowIfNull(visitor);
foreach (var part in bodyParts)
part.Accept(visitor); Defensive patterns
Strategy: validation
Validate before calling
if (visitor is null)
return; // or create a default visitor
multipart.Accept(visitor); Type guard
bool CanTraverse(BodyPart part, BodyPartVisitor? visitor) => part is BodyPartMultipart && visitor is not null;
Try / catch
try {
multipart.Accept(visitor);
} catch (ArgumentNullException ex) when (ex.ParamName == "visitor") {
visitor = new DefaultBodyPartVisitor();
multipart.Accept(visitor);
} Prevention
- Pass the visitor through traversal code as a non-nullable parameter.
- Construct a default/no-op visitor rather than leaving the variable null.
- Add a debug assert or guard clause in your traversal helper before calling Accept.
When it happens
Trigger: Calling multipartBodyPart.Accept(null) while walking a parsed BODYSTRUCTURE tree, usually because the visitor variable was never assigned or a factory method returned null.
Common situations: Generic traversal code that receives the visitor as a parameter itself (possibly null from an outer caller) and forwards it to Accept without checking.
Related errors
AI-assisted analysis of jstedfast/MailKit@9d3859a785 (2026-09-15).
Data as JSON: /api/errors/8d910c79d99be7e2.
Report an issue: GitHub.
Appendix: source
Thrown at MailKit/BodyPartMultipart.cs:156
/// <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.VisitBodyPartMultipart (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)
{
Encode (builder, BodyParts);
builder.Append (' ');
Encode (builder, ContentType.MediaSubtype);
builder.Append (' ');
Encode (builder, ContentType.Parameters);View on GitHub (pinned to 9d3859a785)