litedb-org/LiteDB · error · ArgumentNullException
Value cannot be null. (Parameter 'dict')
Error message
Value cannot be null. (Parameter 'dict')
What it means
Thrown when the BsonDocument constructor that accepts a ConcurrentDictionary<string, BsonValue> receives a null argument. The constructor copies elements from the supplied dictionary into the new document's internal storage, so a null source is rejected early via ArgumentNullException. This is a fail-fast guard before any iteration begins.
Source
Thrown at LiteDB/Document/BsonDocument.cs:22
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using static LiteDB.Constants;
namespace LiteDB
{
public class BsonDocument : BsonValue, IDictionary<string, BsonValue>
{
public BsonDocument()
: base(BsonType.Document, new Dictionary<string, BsonValue>(StringComparer.OrdinalIgnoreCase))
{
}
public BsonDocument(ConcurrentDictionary<string, BsonValue> dict)
: this()
{
if (dict == null) throw new ArgumentNullException(nameof(dict));
foreach(var element in dict)
{
this.Add(element);
}
}
public BsonDocument(IDictionary<string, BsonValue> dict)
: this()
{
if (dict == null) throw new ArgumentNullException(nameof(dict));
foreach (var element in dict)
{
this.Add(element);
}
}
View on GitHub (pinned to f906a5f850)
Solutions
- Check the dictionary for null before constructing the BsonDocument and either supply an empty ConcurrentDictionary or handle the missing-data case explicitly.
- Use the parameterless `new BsonDocument()` when you have no initial data, then add entries individually.
- Trace upstream to find why the ConcurrentDictionary is null and fix the source.
Example fix
// before var doc = new BsonDocument(maybeNullDict); // after var doc = new BsonDocument(maybeNullDict ?? new ConcurrentDictionary<string, BsonValue>());
Defensive patterns
Strategy: validation
Validate before calling
if (dict == null) throw new ArgumentException("Source dictionary is required.");
var doc = new BsonDocument(dict); Type guard
static bool IsValidDocumentSource(ConcurrentDictionary<string, BsonValue> dict) => dict != null;
Try / catch
try { var doc = new BsonDocument(dict); }
catch (ArgumentNullException ex) when (ex.ParamName == "dict")
{ /* log and supply empty doc or rethrow with context */ } Prevention
- Always null-check dictionaries from external sources before constructing BsonDocument.
- Use the parameterless constructor when the source may be absent.
- Enable nullable reference types so the compiler flags potential null passes.
When it happens
Trigger: Calling `new BsonDocument((ConcurrentDictionary<string, BsonValue>)null)` or passing a variable that resolved to null at runtime into the ConcurrentDictionary overload of the BsonDocument constructor.
Common situations: A dictionary retrieved from a cache, config, or upstream service is null because of a cache-miss or deserialization failure and is passed directly to BsonDocument without a null-check. Generic code that wraps dictionary creation and sometimes returns null.
Related errors
- Value cannot be null. (Parameter 'array')
- Value cannot be null. (Parameter 'items')
- Value cannot be null. (Parameter 'doc')
- Value cannot be null. (Parameter 'buffer')
- Value cannot be null. (Parameter 'collection')
AI-assisted analysis of litedb-org/LiteDB@f906a5f850 (2026-08-13).
Data as JSON: /api/errors/854b1bf86163da35.
Report an issue: GitHub.