litedb-org/LiteDB · error · ArgumentNullException

field

Error message

field

What it means

Thrown by VectorWhereNear<K>(Expression<Func<T, K>> field, ...) when the field selector expression is null. This overload resolves the LINQ expression to a BsonExpression via the mapper; a null expression cannot be translated. Reached when callers pass a strongly-typed member selector.

Source

Thrown at LiteDB/Client/Database/LiteQueryable.cs:280

            return this.VectorWhereNear(fieldExpr, target, maxDistance);
        }

        internal ILiteQueryable<T> VectorWhereNear(BsonExpression fieldExpr, float[] target, double maxDistance)
        {
            var filter = CreateVectorSimilarityFilter(fieldExpr, target, maxDistance);

            _query.Where.Add(filter);

            _query.VectorField = fieldExpr.Source;
            _query.VectorTarget = target?.ToArray();
            _query.VectorMaxDistance = maxDistance;

            return this;
        }

        internal ILiteQueryable<T> VectorWhereNear<K>(Expression<Func<T, K>> field, float[] target, double maxDistance)
        {
            if (field == null) throw new ArgumentNullException(nameof(field));

            var fieldExpr = _mapper.GetExpression(field);
            return this.VectorWhereNear(fieldExpr, target, maxDistance);
        }

        internal ILiteQueryableResult<T> VectorTopKNear<K>(Expression<Func<T, K>> field, float[] target, int k)
        {
            var fieldExpr = _mapper.GetExpression(field);
            return this.VectorTopKNear(fieldExpr, target, k);
        }

        internal ILiteQueryableResult<T> VectorTopKNear(string field, float[] target, int k)
        {
            var fieldExpr = BsonExpression.Create($"$.{field}");
            return this.VectorTopKNear(fieldExpr, target, k);
        }

        internal ILiteQueryableResult<T> VectorTopKNear(BsonExpression fieldExpr, float[] target, int k)

View on GitHub (pinned to f906a5f850)

Solutions

  1. Pass a real member selector, e.g. x => x.Embedding.
  2. Null-check the expression before calling if it is built dynamically.
  3. If the field name is only known at runtime, use the string overload instead.

Example fix

// before
var q = col.Query().VectorWhereNear(selectorExpr, vec, dist);

// after
if (selectorExpr == null)
    throw new InvalidOperationException("Vector field selector is required.");
var q = col.Query().VectorWhereNear(selectorExpr, vec, dist);
Defensive patterns

Strategy: validation

Validate before calling

if (field == null)
    throw new ArgumentException("A field selector expression is required.", nameof(field));

Prevention

When it happens

Trigger: Calling VectorWhereNear<ICollection<float>>(null, vec, dist), or passing an expression variable that was conditionally assigned null.

Common situations: Building the selector dynamically and a branch leaves it null, or refactoring from a string field name to a LINQ selector and forgetting to supply it.

Related errors


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