{"record":{"id":"7f07f537ec19d90f","repo":"litedb-org/LiteDB","slug":"value-cannot-be-null-parameter-collection","errorCode":null,"errorMessage":"Value cannot be null. (Parameter 'collection')","messagePattern":"Value cannot be null\\. \\(Parameter 'collection'\\)","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"LiteDB/Client/Vector/LiteCollectionVectorExtensions.cs","lineNumber":35,"sourceCode":"        {\n            return Unwrap(collection).EnsureVectorIndex(expression, options);\n        }\n\n        public static bool EnsureIndex<T, K>(this ILiteCollection<T> collection, Expression<Func<T, K>> keySelector, VectorIndexOptions options)\n        {\n            return Unwrap(collection).EnsureVectorIndex(keySelector, options);\n        }\n\n        public static bool EnsureIndex<T, K>(this ILiteCollection<T> collection, string name, Expression<Func<T, K>> keySelector, VectorIndexOptions options)\n        {\n            return Unwrap(collection).EnsureVectorIndex(name, keySelector, options);\n        }\n\n        private static LiteCollection<T> Unwrap<T>(ILiteCollection<T> collection)\n        {\n            if (collection is null)\n            {\n                throw new ArgumentNullException(nameof(collection));\n            }\n\n            if (collection is LiteCollection<T> concrete)\n            {\n                return concrete;\n            }\n\n            throw new ArgumentException(\"Vector index operations require LiteDB's default collection implementation.\", nameof(collection));\n        }\n    }\n}\n","sourceCodeStart":17,"sourceCodeEnd":47,"githubUrl":"https://github.com/litedb-org/LiteDB/blob/f906a5f850678719e39a39a006cb66dcae563cfa/LiteDB/Client/Vector/LiteCollectionVectorExtensions.cs#L17-L47","documentation":"Thrown by the private Unwrap helper in LiteCollectionVectorExtensions when the collection passed to a vector-index extension method is null. The extension methods (EnsureVectorIndex/EnsureIndex overloads) delegate to the concrete LiteCollection<T>, so a null receiver has no real target to operate on.","triggerScenarios":"Calling collection.EnsureVectorIndex(...) on a null ILiteCollection<T>; retrieving a collection from a disposed database; a DI container injecting null for the collection.","commonSituations":"Using a collection obtained via db.GetCollection<T>(name) after the LiteDatabase was disposed; mock frameworks returning default (null) from a setup that was never configured; conditional collection access where the variable was never assigned.","solutions":["Verify the collection reference is non-null before calling any vector extension; check the LiteDatabase is not disposed.","Ensure the collection is obtained from a live LiteDatabase instance via db.GetCollection<T>(name).","Fix DI/mock setups so the collection is always resolved to a real instance."],"exampleFix":"// before\ncollection.EnsureVectorIndex(x => x.Embedding, opts);\n\n// after\nif (collection is null)\n    throw new InvalidOperationException(\"Collection was not resolved from the database.\");\ncollection.EnsureVectorIndex(x => x.Embedding, opts);","handlingStrategy":"validation","validationCode":"if (collection is null)\n    throw new InvalidOperationException(\"Collection is not initialized.\");\ncollection.EnsureVectorIndex(x => x.Embedding, opts);","typeGuard":"static bool IsLiveCollection<T>(ILiteCollection<T> c) => c is not null;","tryCatchPattern":null,"preventionTips":["Ensure the LiteDatabase is not disposed before obtaining the collection.","Validate DI registrations resolve a non-null collection.","Obtain collections fresh per unit of work rather than caching across dispose cycles."],"tags":["argument-validation","vector","null-check","collection","extension-method"],"backgroundTag":null,"analyzedSha":"f906a5f850678719e39a39a006cb66dcae563cfa","analyzedAt":"2026-08-13T21:56:30.148Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}