litedb-org/LiteDB · error · ArgumentNullException
Value cannot be null. (Parameter 'repository')
Error message
Value cannot be null. (Parameter 'repository')
What it means
Thrown by Unwrap in LiteRepositoryVectorExtensions when the repository passed to a vector EnsureIndex extension is null. The extension forwards to the concrete LiteRepository to access the underlying collections and engine, so a null repository cannot proceed.
Source
Thrown at LiteDB/Client/Vector/LiteRepositoryVectorExtensions.cs:35
{
return Unwrap(repository).EnsureVectorIndex<T>(expression, options, collectionName);
}
public static bool EnsureIndex<T, K>(this ILiteRepository repository, Expression<Func<T, K>> keySelector, VectorIndexOptions options, string collectionName = null)
{
return Unwrap(repository).EnsureVectorIndex<T, K>(keySelector, options, collectionName);
}
public static bool EnsureIndex<T, K>(this ILiteRepository repository, string name, Expression<Func<T, K>> keySelector, VectorIndexOptions options, string collectionName = null)
{
return Unwrap(repository).EnsureVectorIndex<T, K>(name, keySelector, options, collectionName);
}
private static LiteRepository Unwrap(ILiteRepository repository)
{
if (repository is null)
{
throw new ArgumentNullException(nameof(repository));
}
if (repository is LiteRepository liteRepository)
{
return liteRepository;
}
throw new ArgumentException("Vector index operations require LiteDB's default repository implementation.", nameof(repository));
}
}
}
View on GitHub (pinned to f906a5f850)
Solutions
- Confirm the repository is a live LiteRepository before calling the extension.
- Fix DI registration so ILiteRepository resolves to a non-null instance for the scope.
- Avoid holding repository references across dispose boundaries.
Example fix
// before
repo.EnsureVectorIndex<MyDoc, float[]>(x => x.Embedding, opts);
// after
if (repo is null)
throw new InvalidOperationException("Repository is not available.");
repo.EnsureVectorIndex<MyDoc, float[]>(x => x.Embedding, opts); Defensive patterns
Strategy: validation
Validate before calling
if (repo is null)
throw new InvalidOperationException("Repository is not available.");
repo.EnsureVectorIndex<MyDoc, float[]>(x => x.Embedding, opts); Type guard
static bool IsLiveRepository(ILiteRepository r) => r is not null;
Prevention
- Verify DI scopes resolve a non-null repository.
- Avoid holding repository references across disposal.
- Initialize repositories eagerly in composition roots.
When it happens
Trigger: Calling repo.EnsureVectorIndex<T,K>(...) on a null ILiteRepository; repository obtained from a scope that was already disposed; DI misconfiguration injecting null.
Common situations: Transient/scoped repository lifetimes resolving to null after disposal; test harnesses not initializing the repository; conditional instantiation branches skipping assignment.
Related errors
- Value cannot be null. (Parameter 'collection')
- Value cannot be null. (Parameter 'source')
- Vector index operations require LiteDB's default repository
- The current collection implementation does not support vecto
- Vector index operations require LiteDB's default collection
AI-assisted analysis of litedb-org/LiteDB@f906a5f850 (2026-08-13).
Data as JSON: /api/errors/62d5c5d43045e116.
Report an issue: GitHub.