{"record":{"id":"90a8b6321db5e5c0","repo":"jstedfast/MailKit","slug":"value-cannot-be-null-parameter-metadata","errorCode":null,"errorMessage":"Value cannot be null. (Parameter 'metadata')","messagePattern":"Value cannot be null\\. \\(Parameter 'metadata'\\)","errorType":"validation","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"MailKit/Net/Imap/ImapFolder.cs","lineNumber":3473,"sourceCode":"\t\t/// <exception cref=\"ImapCommandException\">\n\t\t/// The server replied with a NO or BAD response.\n\t\t/// </exception>\n\t\tpublic override async Task<MetadataCollection> GetMetadataAsync (MetadataOptions options, IEnumerable<MetadataTag> tags, CancellationToken cancellationToken = default)\n\t\t{\n\t\t\tvar ic = QueueGetMetadataCommand (options, tags, cancellationToken);\n\n\t\t\tif (ic == null)\n\t\t\t\treturn new MetadataCollection ();\n\n\t\t\tawait Engine.RunAsync (ic).ConfigureAwait (false);\n\n\t\t\treturn ProcessGetMetadataResponse (ic, options);\n\t\t}\n\n\t\tImapCommand? QueueSetMetadataCommand (MetadataCollection metadata, CancellationToken cancellationToken)\n\t\t{\n\t\t\tif (metadata == null)\n\t\t\t\tthrow new ArgumentNullException (nameof (metadata));\n\n\t\t\tCheckState (false, false);\n\n\t\t\tif ((Engine.Capabilities & ImapCapabilities.Metadata) == 0)\n\t\t\t\tthrow new NotSupportedException (\"The IMAP server does not support the METADATA extension.\");\n\n\t\t\tif (metadata.Count == 0)\n\t\t\t\treturn null;\n\n\t\t\tvar command = new StringBuilder (\"SETMETADATA %F (\");\n\t\t\tvar args = new List<object> {\n\t\t\t\tthis\n\t\t\t};\n\n\t\t\tfor (int i = 0; i < metadata.Count; i++) {\n\t\t\t\tif (i > 0)\n\t\t\t\t\tcommand.Append (' ');\n","sourceCodeStart":3455,"sourceCodeEnd":3491,"githubUrl":"https://github.com/jstedfast/MailKit/blob/9d3859a7855e3e17582c07fd01972b8e262bf176/MailKit/Net/Imap/ImapFolder.cs#L3455-L3491","documentation":"QueueSetMetadataCommand throws ArgumentNullException when its MetadataCollection argument is null. Setting metadata requires a collection instance; to clear entries you set null values inside the collection rather than passing a null collection.","triggerScenarios":"Calling ImapFolder.SetMetadata(MetadataCollection, CancellationToken) with a null collection, often from an unassigned variable or a null deserialized payload.","commonSituations":"Intending to 'delete all metadata' by passing null instead of a collection with null-valued entries; building metadata conditionally and leaving the variable null.","solutions":["Pass a new MetadataCollection() (empty collections simply queue nothing)","To erase a metadata entry, add the tag with a null value instead of passing a null collection","Validate the collection non-null before calling"],"exampleFix":"// before\nMetadataCollection metadata = null;\nfolder.SetMetadata(metadata, ct);\n// after\nvar metadata = new MetadataCollection { { MetadataTag.Comment, null } }; // clears /comment\nfolder.SetMetadata(metadata, ct);","handlingStrategy":"validation","validationCode":"if (metadata == null)\n    throw new ArgumentException(\"Metadata collection is required\", nameof(metadata));","typeGuard":"bool IsValidMetadata(MetadataCollection m) => m != null;","tryCatchPattern":"try {\n    folder.SetMetadata(metadata, ct);\n} catch (ArgumentNullException ex) {\n    logger.LogError(ex, \"Null metadata collection passed to SetMetadata\");\n    throw;\n}","preventionTips":["Remember empty MetadataCollection is valid; null is not","Delete entries by setting their value to null inside the collection","Initialize MetadataCollection at declaration with an initializer","Use nullable reference types to surface unassigned collections"],"tags":["imap","metadata","null-argument","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"}