{"record":{"id":"3cc0af029be82a86","repo":"litedb-org/LiteDB","slug":"value-cannot-be-null-parameter-doc","errorCode":null,"errorMessage":"Value cannot be null. (Parameter 'doc')","messagePattern":"Value cannot be null\\. \\(Parameter 'doc'\\)","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"LiteDB/Document/Bson/BsonSerializer.cs","lineNumber":20,"sourceCode":"using System;\nusing System.Collections.Generic;\nusing System.IO;\nusing static LiteDB.Constants;\n\nnamespace LiteDB\n{\n    /// <summary>\n    /// Class to call method for convert BsonDocument to/from byte[] - based on http://bsonspec.org/spec.html\n    /// In v5 this class use new BufferRead/Writer to work with byte[] segments. This class are just a shortchut\n    /// </summary>\n    public class BsonSerializer\n    {\n        /// <summary>\n        /// Serialize BsonDocument into a binary array\n        /// </summary>\n        public static byte[] Serialize(BsonDocument doc)\n        {\n            if (doc == null) throw new ArgumentNullException(nameof(doc));\n\n            var buffer = new byte[doc.GetBytesCount(true)]; \n\n            using (var writer = new BufferWriter(buffer))\n            {\n                writer.WriteDocument(doc, false);\n            }\n\n            return buffer;\n        }\n\n        /// <summary>\n        /// Deserialize binary data into BsonDocument\n        /// </summary>\n        public static BsonDocument Deserialize(byte[] buffer, bool utcDate = false, HashSet<string> fields = null)\n        {\n            if (buffer == null || buffer.Length == 0) throw new ArgumentNullException(nameof(buffer));\n","sourceCodeStart":2,"sourceCodeEnd":38,"githubUrl":"https://github.com/litedb-org/LiteDB/blob/f906a5f850678719e39a39a006cb66dcae563cfa/LiteDB/Document/Bson/BsonSerializer.cs#L2-L38","documentation":"Thrown by BsonSerializer.Serialize when the BsonDocument argument is null. Serialization walks the document's element list to write bytes, so a null document has no payload to emit. The guard prevents a NullReferenceException deeper in the buffer writer.","triggerScenarios":"Calling BsonSerializer.Serialize(null); passing a document obtained from a query that returned null; serializing a deserialized result whose source data was absent.","commonSituations":"Manual BSON conversion for custom transport layers; cache stores that serialize documents; mapping pipelines where an upstream stage produced null.","solutions":["Null-check the document before serializing and handle absence explicitly.","Ensure query/mapping stages return BsonDocument.Empty or a sentinel rather than null.","Guard at the boundary where documents enter the serialization path."],"exampleFix":"// before\nvar bytes = BsonSerializer.Serialize(doc);\n\n// after\nvar bytes = doc is null ? Array.Empty<byte>() : BsonSerializer.Serialize(doc);","handlingStrategy":"validation","validationCode":"if (doc is null)\n    throw new ArgumentException(\"Document is required for serialization.\", nameof(doc));\nvar bytes = BsonSerializer.Serialize(doc);","typeGuard":"static bool IsSerializable(BsonDocument d) => d is not null;","tryCatchPattern":null,"preventionTips":["Ensure mapping/query stages return BsonDocument.Empty instead of null.","Null-check documents at the serialization boundary.","Use document.GetValueOrDefault for optional fields."],"tags":["argument-validation","bson","serialization","null-check"],"backgroundTag":null,"analyzedSha":"f906a5f850678719e39a39a006cb66dcae563cfa","analyzedAt":"2026-08-13T21:56:30.148Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}