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

  1. Confirm the repository is a live LiteRepository before calling the extension.
  2. Fix DI registration so ILiteRepository resolves to a non-null instance for the scope.
  3. 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

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


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