{"record":{"id":"616ae8fb89c91823","repo":"jstedfast/MailKit","slug":"visitor","errorCode":null,"errorMessage":"visitor","messagePattern":"visitor","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"MailKit/BodyPartBasic.cs","lineNumber":231,"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.VisitBodyPartBasic (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\tEncode (builder, ContentType);\n\t\t\tbuilder.Append (' ');\n\t\t\tEncode (builder, ContentId);\n\t\t\tbuilder.Append (' ');\n\t\t\tEncode (builder, ContentDescription);","sourceCodeStart":213,"sourceCodeEnd":249,"githubUrl":"https://github.com/jstedfast/MailKit/blob/9d3859a7855e3e17582c07fd01972b8e262bf176/MailKit/BodyPartBasic.cs#L213-L249","documentation":"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\".","triggerScenarios":"Calling part.Accept(null) on any BodyPart (including BodyPartBasic) — usually when the visitor variable was never initialized or a factory returned null.","commonSituations":"Building a BodyPartVisitor subclass conditionally and forgetting to instantiate it; dependency-injection returning null in tests; refactoring that removed the new MyVisitor() call.","solutions":["Instantiate the visitor before calling Accept: part.Accept(new MyVisitor())","Guard with a null check and skip traversal when no visitor is needed","Fix the visitor factory/DI registration so it never returns null"],"exampleFix":"// before\nBodyPartVisitor visitor = GetVisitor(); // returned null\npart.Accept(visitor);\n// after\nvar visitor = GetVisitor() ?? new DefaultBodyPartVisitor();\npart.Accept(visitor);","handlingStrategy":"validation","validationCode":"if (visitor == null) throw new InvalidOperationException(\"A BodyPartVisitor must be provided to traverse the body structure.\");","typeGuard":"static bool HasVisitor(BodyPartVisitor? v) => v is not null;","tryCatchPattern":"try { part.Accept(visitor); }\ncatch (ArgumentNullException ex) when (ex.ParamName == \"visitor\") { /* construct or fix the visitor, then retry */ }","preventionTips":["Always pass a concrete visitor instance: part.Accept(new MyVisitor())","Check DI/factory code that supplies visitors so it never returns null","Make visitor fields readonly and initialize them at declaration"],"tags":["mailkit","null-argument","visitor-pattern","bodystructure"],"backgroundTag":"null-argument","analyzedSha":"9d3859a7855e3e17582c07fd01972b8e262bf176","analyzedAt":"2026-09-15T15:46:11.592Z","contentChangedAt":"2026-09-15T15:46:11.592Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}