jstedfast/MailKit · error · ArgumentNullException
visitor
Error message
visitor
What it means
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.
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).
Example fix
// before
part.Accept(visitor); // visitor may be null
// after
if (visitor != null)
part.Accept(visitor); Defensive patterns
Strategy: validation
Validate before calling
if (visitor is null)
throw new InvalidOperationException("No visitor configured for body structure traversal.");
messageBodyPart.Accept(visitor); Type guard
bool CanAccept(BodyPart part, BodyPartVisitor? visitor) => part is BodyPartMessage && visitor is not null;
Try / catch
try {
part.Accept(visitor);
} catch (ArgumentNullException ex) when (ex.ParamName == "visitor") {
logger.LogWarning("Traversal skipped: visitor was null");
} Prevention
- 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.
When it happens
Trigger: 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).
Common situations: 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.
Related errors
AI-assisted analysis of jstedfast/MailKit@9d3859a785 (2026-09-15).
Data as JSON: /api/errors/f783a33463fe6784.
Report an issue: GitHub.
Appendix: source
Thrown at MailKit/BodyPartMessage.cs:120
/// <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.VisitBodyPartMessage (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, Envelope);
builder.Append (' ');View on GitHub (pinned to 9d3859a785)