litedb-org/LiteDB · error · ArgumentNullException
Value cannot be null. (Parameter 'doc')
Error message
Value cannot be null. (Parameter 'doc')
What it means
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.
Source
Thrown at LiteDB/Document/Bson/BsonSerializer.cs:20
using System;
using System.Collections.Generic;
using System.IO;
using static LiteDB.Constants;
namespace LiteDB
{
/// <summary>
/// Class to call method for convert BsonDocument to/from byte[] - based on http://bsonspec.org/spec.html
/// In v5 this class use new BufferRead/Writer to work with byte[] segments. This class are just a shortchut
/// </summary>
public class BsonSerializer
{
/// <summary>
/// Serialize BsonDocument into a binary array
/// </summary>
public static byte[] Serialize(BsonDocument doc)
{
if (doc == null) throw new ArgumentNullException(nameof(doc));
var buffer = new byte[doc.GetBytesCount(true)];
using (var writer = new BufferWriter(buffer))
{
writer.WriteDocument(doc, false);
}
return buffer;
}
/// <summary>
/// Deserialize binary data into BsonDocument
/// </summary>
public static BsonDocument Deserialize(byte[] buffer, bool utcDate = false, HashSet<string> fields = null)
{
if (buffer == null || buffer.Length == 0) throw new ArgumentNullException(nameof(buffer));
View on GitHub (pinned to f906a5f850)
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.
Example fix
// before var bytes = BsonSerializer.Serialize(doc); // after var bytes = doc is null ? Array.Empty<byte>() : BsonSerializer.Serialize(doc);
Defensive patterns
Strategy: validation
Validate before calling
if (doc is null)
throw new ArgumentException("Document is required for serialization.", nameof(doc));
var bytes = BsonSerializer.Serialize(doc); Type guard
static bool IsSerializable(BsonDocument d) => d is not null;
Prevention
- Ensure mapping/query stages return BsonDocument.Empty instead of null.
- Null-check documents at the serialization boundary.
- Use document.GetValueOrDefault for optional fields.
When it happens
Trigger: Calling BsonSerializer.Serialize(null); passing a document obtained from a query that returned null; serializing a deserialized result whose source data was absent.
Common situations: Manual BSON conversion for custom transport layers; cache stores that serialize documents; mapping pipelines where an upstream stage produced null.
Related errors
- Value cannot be null. (Parameter 'buffer')
- Value cannot be null. (Parameter 'array')
- Value cannot be null. (Parameter 'items')
- Value cannot be null. (Parameter 'collection')
- Value cannot be null. (Parameter 'collection')
AI-assisted analysis of litedb-org/LiteDB@f906a5f850 (2026-08-13).
Data as JSON: /api/errors/3cc0af029be82a86.
Report an issue: GitHub.