litedb-org/LiteDB · error · ArgumentNullException

Value cannot be null. (Parameter 'field')

Error message

Value cannot be null. (Parameter 'field')

What it means

Query.GT(string field, BsonValue value) builds a BsonExpression '$field > $value' for a greater-than filter. At Query.cs:89 it throws ArgumentNullException(nameof(field)) when field is null, empty, or whitespace, because the field path is required to form the comparison. The check uses IsNullOrWhiteSpace, so blank strings are rejected identically to null. LiteDB throws on the API boundary to surface the real defect.

Source

Thrown at LiteDB/Client/Structures/Query.cs:89

            return BsonExpression.Create($"{field} < {value ?? BsonValue.Null}");
        }

        /// <summary>
        /// Returns all documents that value are less than or equals value (&lt;=)
        /// </summary>
        public static BsonExpression LTE(string field, BsonValue value)
        {
            if (field.IsNullOrWhiteSpace()) throw new ArgumentNullException(nameof(field));

            return BsonExpression.Create($"{field} <= {value ?? BsonValue.Null}");
        }

        /// <summary>
        /// Returns all document that value are greater than value (&gt;)
        /// </summary>
        public static BsonExpression GT(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 greater than or equals value (&gt;=)
        /// </summary>
        public static BsonExpression GTE(string field, BsonValue value)
        {
            if (field.IsNullOrWhiteSpace()) throw new ArgumentNullException(nameof(field));

            return BsonExpression.Create($"{field} >= {value ?? BsonValue.Null}");
        }

        /// <summary>
        /// Returns all document that values are between "start" and "end" values (BETWEEN)
        /// </summary>
        public static BsonExpression Between(string field, BsonValue start, BsonValue end)

View on GitHub (pinned to f906a5f850)

Solutions

  1. Use a concrete field path: Query.GT("amount", threshold).
  2. Resolve/validate the alias to a real field name before calling Query.GT.
  3. Unit-test filter builders with empty/null inputs to catch the regression early.

Example fix

// before
var q = Query.GT(resolved ?? "", threshold);

// after
if (string.IsNullOrWhiteSpace(resolved))
    throw new InvalidOperationException("Unknown filter alias.");
var q = Query.GT(resolved, threshold);
Defensive patterns

Strategy: validation

Validate before calling

if (string.IsNullOrWhiteSpace(field))
    throw new InvalidOperationException("Field alias could not be resolved.");
var q = Query.GT(field, value);

Type guard

static bool IsValidField(string field) => !string.IsNullOrWhiteSpace(field);

Prevention

When it happens

Trigger: Calling Query.GT(null, min), Query.GT("", min), or Query.GT(" ", min). Common when the field name is computed from data and the computation produced no output.

Common situations: A dynamic sort/filter builder that derives the field from a runtime alias mapping where the alias was missing; passing a value into the field slot by mistake.

Related errors


AI-assisted analysis of litedb-org/LiteDB@f906a5f850 (2026-08-13). Data as JSON: /api/errors/6fa18cb39614651e. Report an issue: GitHub.