litedb-org/LiteDB · error · ArgumentNullException

Value cannot be null. (Parameter 'source')

Error message

Value cannot be null. (Parameter 'source')

What it means

Thrown by Unwrap in LiteQueryableVectorExtensions when the source passed to a vector query extension (e.g., TopKNear) is null. The extension forwards to the concrete LiteQueryable<T>, so a null queryable has no engine backing to perform the vector search.

Source

Thrown at LiteDB/Client/Vector/LiteQueryableVectorExtensions.cs:52

        {
            return Unwrap(source).VectorTopKNear(field, target, k);
        }

        public static ILiteQueryableResult<T> TopKNear<T>(this ILiteQueryable<T> source, string field, float[] target, int k)
        {
            return Unwrap(source).VectorTopKNear(field, target, k);
        }

        public static ILiteQueryableResult<T> TopKNear<T>(this ILiteQueryable<T> source, BsonExpression fieldExpr, float[] target, int k)
        {
            return Unwrap(source).VectorTopKNear(fieldExpr, target, k);
        }

        private static LiteQueryable<T> Unwrap<T>(ILiteQueryable<T> source)
        {
            if (source is null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            if (source is LiteQueryable<T> liteQueryable)
            {
                return liteQueryable;
            }

            throw new ArgumentException("Vector operations require LiteDB's default queryable implementation.", nameof(source));
        }
    }
}

View on GitHub (pinned to f906a5f850)

Solutions

  1. Confirm the ILiteQueryable<T> reference is non-null and the owning LiteDatabase is open before calling TopKNear.
  2. Obtain the queryable fresh from a live collection each time rather than caching across dispose cycles.
  3. Guard the call site with a null check and a clear domain error.

Example fix

// before
var results = query.TopKNear("Embedding", vec, 5);

// after
if (query is null)
    throw new InvalidOperationException("Query is not initialized.");
var results = query.TopKNear("Embedding", vec, 5);
Defensive patterns

Strategy: validation

Validate before calling

if (query is null)
    throw new InvalidOperationException("Query is not initialized.");
var results = query.TopKNear("Embedding", vec, 5);

Type guard

static bool IsLiveQuery<T>(ILiteQueryable<T> q) => q is not null;

Prevention

When it happens

Trigger: Calling query.TopKNear(...) where query is null; chaining TopKNear on a query obtained from a disposed LiteDatabase; a LINQ expression that yielded no queryable instance.

Common situations: Using db.GetCollection<T>().Query() after the database was disposed; conditional query construction that left the variable null; DI-injected queryables not configured.

Related errors


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