litedb-org/LiteDB · error · ArgumentNullException
field
Error message
field
What it means
Query.EQ(string field, BsonValue value) builds a BsonExpression of the form '$field = $value' for an equality filter. At Query.cs:59 it throws ArgumentNullException(nameof(field)) whenever field is null, empty, or whitespace, because such a value cannot be interpolated into a valid BsonExpression and would later fail at parse time with a confusing error. LiteDB fails fast on the public API surface so the caller sees the real cause. The check uses IsNullOrWhiteSpace, so a string of spaces is treated the same as null.
Source
Thrown at LiteDB/Client/Structures/Query.cs:59
return query;
}
/// <summary>
/// Returns all documents
/// </summary>
public static Query All(string field, int order = Ascending)
{
var query = new Query();
query.OrderBy.Add(new QueryOrder(BsonExpression.Create(field), order));
return query;
}
/// <summary>
/// Returns all documents that value are equals to value (=)
/// </summary>
public static BsonExpression EQ(string field, BsonValue value)
{
if (field.IsNullOrWhiteSpace()) throw new ArgumentNullException(nameof(field));
return BsonExpression.Create($"{field} = {value ?? BsonValue.Null}");
}
/// <summary>
/// Returns all documents that value are less than value (<)
/// </summary>
public static BsonExpression LT(string field, BsonValue value)
{
if (field.IsNullOrWhiteSpace()) throw new ArgumentNullException(nameof(field));
return BsonExpression.Create($"{field} < {value ?? BsonValue.Null}");
}
/// <summary>
/// Returns all documents that value are less than or equals value (<=)
/// </summary>
public static BsonExpression LTE(string field, BsonValue value)View on GitHub (pinned to f906a5f850)
Solutions
- Pass a concrete non-empty document field path such as Query.EQ("_id", id).
- Guard the variable first: if (name.IsNullOrWhiteSpace()) return; before calling Query.EQ.
- Centralize field names in constants so they can never silently become empty.
Example fix
// before
var q = Query.EQ(fieldName, age); // fieldName may be null/empty
// after
if (string.IsNullOrWhiteSpace(fieldName))
throw new InvalidOperationException("fieldName must be supplied by the caller.");
var q = Query.EQ(fieldName, age); Defensive patterns
Strategy: validation
Validate before calling
if (string.IsNullOrWhiteSpace(field))
throw new ArgumentException("Field name is required.", nameof(field));
var q = Query.EQ(field, value); Type guard
static bool IsValidField(string field) => !string.IsNullOrWhiteSpace(field);
Prevention
- Validate field names at the API/controller boundary before they reach the query layer.
- Store document field names as constants so they cannot silently become empty.
- Unit-test query builders with null and empty-string field inputs.
When it happens
Trigger: Calling Query.EQ(null, 1), Query.EQ("", 1), or Query.EQ(" ", 1). Also when the field name is loaded from configuration, a dictionary lookup, or JSON deserialization that yields null/empty.
Common situations: Field name read from appsettings or an environment variable that was not set; a property name built dynamically (e.g. char-by-char or via a loop) that produced an empty string; a refactor that swapped argument order and passed the value where the field belongs.
Related errors
- Value cannot be null. (Parameter 'field')
- Value cannot be null. (Parameter 'arrayField')
- Value cannot be null. (Parameter 'left')
- At least two Query should be passed
- command
AI-assisted analysis of litedb-org/LiteDB@f906a5f850 (2026-08-13).
Data as JSON: /api/errors/c9353cc7a882efed.
Report an issue: GitHub.