litedb-org/LiteDB · error · ArgumentNullException
Value cannot be null. (Parameter 'buffer')
Error message
Value cannot be null. (Parameter 'buffer')
What it means
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.
Source
Thrown at LiteDB/Document/Bson/BsonSerializer.cs:37
{
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));
using (var reader = new BufferReader(buffer, utcDate))
{
return reader.ReadDocument(fields).GetValue();
}
}
}
}View on GitHub (pinned to f906a5f850)
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.
Example fix
// before
var doc = BsonSerializer.Deserialize(buffer);
// after
if (buffer is null || buffer.Length == 0)
throw new ArgumentException("Cannot deserialize an empty BSON buffer.", nameof(buffer));
var doc = BsonSerializer.Deserialize(buffer); Defensive patterns
Strategy: validation
Validate before calling
if (buffer is null || buffer.Length == 0)
throw new ArgumentException("Buffer is empty or null.", nameof(buffer));
var doc = BsonSerializer.Deserialize(buffer); Type guard
static bool IsNonEmptyBuffer(byte[] b) => b is not null && b.Length > 0;
Prevention
- 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.
When it happens
Trigger: 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.
Common situations: Reading BSON blobs from external storage that may be empty; deserializing cached payloads that were never populated; network/transport errors returning empty buffers.
Related errors
- Value cannot be null. (Parameter 'doc')
- 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/1c743ccfb60f3f8f.
Report an issue: GitHub.