jstedfast/MailKit · error · ArgumentNullException

visitor

Error message

visitor

What it means

BodyPartBasic.Accept(BodyPartVisitor) throws ArgumentNullException when the visitor parameter is null. This method is part of the visitor pattern used to walk a BODYSTRUCTURE tree; a null visitor would make the dispatch a no-op, so MailKit rejects it eagerly. The message is the parameter name "visitor".

Solutions

  1. Instantiate the visitor before calling Accept: part.Accept(new MyVisitor())
  2. Guard with a null check and skip traversal when no visitor is needed
  3. Fix the visitor factory/DI registration so it never returns null

Example fix

// before
BodyPartVisitor visitor = GetVisitor(); // returned null
part.Accept(visitor);
// after
var visitor = GetVisitor() ?? new DefaultBodyPartVisitor();
part.Accept(visitor);
Defensive patterns

Strategy: validation

Validate before calling

if (visitor == null) throw new InvalidOperationException("A BodyPartVisitor must be provided to traverse the body structure.");

Type guard

static bool HasVisitor(BodyPartVisitor? v) => v is not null;

Try / catch

try { part.Accept(visitor); }
catch (ArgumentNullException ex) when (ex.ParamName == "visitor") { /* construct or fix the visitor, then retry */ }

Prevention

When it happens

Trigger: Calling part.Accept(null) on any BodyPart (including BodyPartBasic) — usually when the visitor variable was never initialized or a factory returned null.

Common situations: Building a BodyPartVisitor subclass conditionally and forgetting to instantiate it; dependency-injection returning null in tests; refactoring that removed the new MyVisitor() call.

Related errors


AI-assisted analysis of jstedfast/MailKit@9d3859a785 (2026-09-15). Data as JSON: /api/errors/616ae8fb89c91823. Report an issue: GitHub.

Appendix: source

Thrown at MailKit/BodyPartBasic.cs:231

		/// <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.VisitBodyPartBasic (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, ContentType);
			builder.Append (' ');
			Encode (builder, ContentId);
			builder.Append (' ');
			Encode (builder, ContentDescription);

View on GitHub (pinned to 9d3859a785)