{"record":{"id":"1c743ccfb60f3f8f","repo":"litedb-org/LiteDB","slug":"value-cannot-be-null-parameter-buffer","errorCode":null,"errorMessage":"Value cannot be null. (Parameter 'buffer')","messagePattern":"Value cannot be null\\. \\(Parameter 'buffer'\\)","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"LiteDB/Document/Bson/BsonSerializer.cs","lineNumber":37,"sourceCode":"        {\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\n            using (var reader = new BufferReader(buffer, utcDate))\n            {\n                return reader.ReadDocument(fields).GetValue();\n            }\n        }\n    }\n}","sourceCodeStart":19,"sourceCodeEnd":45,"githubUrl":"https://github.com/litedb-org/LiteDB/blob/f906a5f850678719e39a39a006cb66dcae563cfa/LiteDB/Document/Bson/BsonSerializer.cs#L19-L45","documentation":"Thrown by BsonSerializer.Deserialize when buffer is null or has zero length. Deserialization reads BSON framing from the byte array, and an empty/null buffer contains no valid document header to parse. Note the same exception type (ArgumentNullException) is used for both null and empty cases.","triggerScenarios":"Calling BsonSerializer.Deserialize(null); passing a zero-length byte array from a failed network read or empty DB cell; feeding truncated/corrupt data that resolved to empty.","commonSituations":"Reading BSON blobs from external storage that may be empty; deserializing cached payloads that were never populated; network/transport errors returning empty buffers.","solutions":["Check buffer is non-null and Length > 0 before deserializing.","Validate upstream data sources return a well-formed payload or a known sentinel.","Distinguish empty-buffer from corrupt-buffer handling at the transport layer."],"exampleFix":"// before\nvar doc = BsonSerializer.Deserialize(buffer);\n\n// after\nif (buffer is null || buffer.Length == 0)\n    throw new ArgumentException(\"Cannot deserialize an empty BSON buffer.\", nameof(buffer));\nvar doc = BsonSerializer.Deserialize(buffer);","handlingStrategy":"validation","validationCode":"if (buffer is null || buffer.Length == 0)\n    throw new ArgumentException(\"Buffer is empty or null.\", nameof(buffer));\nvar doc = BsonSerializer.Deserialize(buffer);","typeGuard":"static bool IsNonEmptyBuffer(byte[] b) => b is not null && b.Length > 0;","tryCatchPattern":null,"preventionTips":["Validate buffer length at the transport/storage boundary.","Distinguish empty-buffer from corrupt-buffer handling explicitly.","Ensure upstream sources return well-formed payloads or a known sentinel."],"tags":["argument-validation","bson","deserialization","null-check","empty-buffer"],"backgroundTag":null,"analyzedSha":"f906a5f850678719e39a39a006cb66dcae563cfa","analyzedAt":"2026-08-13T21:56:30.148Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}