litedb-org/LiteDB · error · ArgumentNullException
Value cannot be null. (Parameter 'left')
Error message
Value cannot be null. (Parameter 'left')
What it means
Query.And(BsonExpression left, BsonExpression right) combines two filters into '(left AND right)'. At Query.cs:195 it throws ArgumentNullException(nameof(left)) when the left operand is null, because interpolating null.Source would NullReferenceException later. The right operand is checked immediately after at line 196. Both sides must be non-null BsonExpression instances produced by the other Query builders.
Source
Thrown at LiteDB/Client/Structures/Query.cs:195
/// <summary>
/// Returns all documents that has value in values list (IN)
/// </summary>
public static BsonExpression In(string field, IEnumerable<BsonValue> values)
{
return In(field, new BsonArray(values));
}
/// <summary>
/// Get all operands to works with array or enumerable values
/// </summary>
public static QueryAny Any() => new QueryAny();
/// <summary>
/// Returns document that exists in BOTH queries results. If both queries has indexes, left query has index preference (other side will be run in full scan)
/// </summary>
public static BsonExpression And(BsonExpression left, BsonExpression right)
{
if (left == null) throw new ArgumentNullException(nameof(left));
if (right == null) throw new ArgumentNullException(nameof(right));
return $"({left.Source} AND {right.Source})";
}
/// <summary>
/// Returns document that exists in ALL queries results.
/// </summary>
public static BsonExpression And(params BsonExpression[] queries)
{
if (queries == null || queries.Length < 2) throw new ArgumentException("At least two Query should be passed");
var left = queries[0];
for (int i = 1; i < queries.Length; i++)
{
left = And(left, queries[i]);
}View on GitHub (pinned to f906a5f850)
Solutions
- Ensure both operands are non-null before calling: use Query.All() as a neutral identity when a side is optional.
- Return Query.All("_id") instead of null from helper builders.
- Null-check each operand and short-circuit.
Example fix
// before
var q = Query.And(maybeLeft, right); // maybeLeft can be null
// after
var left = maybeLeft ?? Query.All("_id");
var q = Query.And(left, right); Defensive patterns
Strategy: validation
Validate before calling
var safeLeft = left ?? Query.All("_id");
var q = Query.And(safeLeft, right); Type guard
static bool IsComposable(BsonExpression e) => e is not null;
Prevention
- Make filter-builder helpers never return null — use Query.All() as identity.
- Null-check operands when composing optional filters.
- Use Query.All() as the neutral left operand for conditional ANDs.
When it happens
Trigger: Calling Query.And(null, expr) or Query.And(maybeNull, expr) where the left side came from a conditional that returned null. Note this binary overload is also called internally by the params overload, so a null element inside a params array surfaces here too.
Common situations: Conditionally building a left filter (e.g. only when a flag is set) that yields null; composing results of helper methods that can return null on empty input.
Related errors
- At least two Query should be passed
- field
- Value cannot be null. (Parameter 'field')
- Value cannot be null. (Parameter 'arrayField')
- command
AI-assisted analysis of litedb-org/LiteDB@f906a5f850 (2026-08-13).
Data as JSON: /api/errors/7b6448c6fc294ecf.
Report an issue: GitHub.