{"record":{"id":"044e092079b3d65c","repo":"jstedfast/MailKit","slug":"visitor-bodyparttext","errorCode":null,"errorMessage":"visitor","messagePattern":"visitor","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"MailKit/BodyPartText.cs","lineNumber":123,"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.VisitBodyPartText (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\tbase.Encode (builder);\n\n\t\t\tbuilder.Append (' ');\n\t\t\tEncode (builder, Lines);\n\t\t}","sourceCodeStart":105,"sourceCodeEnd":141,"githubUrl":"https://github.com/jstedfast/MailKit/blob/9d3859a7855e3e17582c07fd01972b8e262bf176/MailKit/BodyPartText.cs#L105-L141","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nBodyPartVisitor visitor = null;\nif (fetchText) visitor = new MyVisitor();\npart.Accept(visitor);\n// after\nvar visitor = new MyVisitor(); // always create, control behavior with flags inside\npart.Accept(visitor);","handlingStrategy":"validation","validationCode":"if (visitor is null)\n    visitor = new DefaultBodyPartVisitor();\ntextPart.Accept(visitor);","typeGuard":"bool CanAcceptText(BodyPart part, BodyPartVisitor? visitor) => part is BodyPartText && visitor is not null;","tryCatchPattern":"try {\n    part.Accept(visitor);\n} catch (ArgumentNullException ex) when (ex.ParamName == \"visitor\") {\n    // visitor was null; create one and retry or skip\n    part.Accept(new DefaultBodyPartVisitor());\n}","preventionTips":["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."],"tags":["null-reference","visitor-pattern","argument-validation","mailkit"],"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"}