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
- Confirm the ILiteQueryable<T> reference is non-null and the owning LiteDatabase is open before calling TopKNear.
- Obtain the queryable fresh from a live collection each time rather than caching across dispose cycles.
- 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
- Obtain queryables from a live collection each time rather than caching.
- Ensure the LiteDatabase is open before calling Query().
- Null-check query references in conditional construction paths.
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
- Value cannot be null. (Parameter 'collection')
- Value cannot be null. (Parameter 'repository')
- Vector operations require LiteDB's default queryable impleme
- Vector index operations require LiteDB's default collection
- Vector index operations require LiteDB's default repository
AI-assisted analysis of litedb-org/LiteDB@f906a5f850 (2026-08-13).
Data as JSON: /api/errors/034f7ed6df993755.
Report an issue: GitHub.