{"record":{"id":"034f7ed6df993755","repo":"litedb-org/LiteDB","slug":"value-cannot-be-null-parameter-source","errorCode":null,"errorMessage":"Value cannot be null. (Parameter 'source')","messagePattern":"Value cannot be null\\. \\(Parameter 'source'\\)","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"LiteDB/Client/Vector/LiteQueryableVectorExtensions.cs","lineNumber":52,"sourceCode":"        {\n            return Unwrap(source).VectorTopKNear(field, target, k);\n        }\n\n        public static ILiteQueryableResult<T> TopKNear<T>(this ILiteQueryable<T> source, string field, float[] target, int k)\n        {\n            return Unwrap(source).VectorTopKNear(field, target, k);\n        }\n\n        public static ILiteQueryableResult<T> TopKNear<T>(this ILiteQueryable<T> source, BsonExpression fieldExpr, float[] target, int k)\n        {\n            return Unwrap(source).VectorTopKNear(fieldExpr, target, k);\n        }\n\n        private static LiteQueryable<T> Unwrap<T>(ILiteQueryable<T> source)\n        {\n            if (source is null)\n            {\n                throw new ArgumentNullException(nameof(source));\n            }\n\n            if (source is LiteQueryable<T> liteQueryable)\n            {\n                return liteQueryable;\n            }\n\n            throw new ArgumentException(\"Vector operations require LiteDB's default queryable implementation.\", nameof(source));\n        }\n    }\n}\n","sourceCodeStart":34,"sourceCodeEnd":64,"githubUrl":"https://github.com/litedb-org/LiteDB/blob/f906a5f850678719e39a39a006cb66dcae563cfa/LiteDB/Client/Vector/LiteQueryableVectorExtensions.cs#L34-L64","documentation":"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.","triggerScenarios":"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.","commonSituations":"Using db.GetCollection<T>().Query() after the database was disposed; conditional query construction that left the variable null; DI-injected queryables not configured.","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."],"exampleFix":"// before\nvar results = query.TopKNear(\"Embedding\", vec, 5);\n\n// after\nif (query is null)\n    throw new InvalidOperationException(\"Query is not initialized.\");\nvar results = query.TopKNear(\"Embedding\", vec, 5);","handlingStrategy":"validation","validationCode":"if (query is null)\n    throw new InvalidOperationException(\"Query is not initialized.\");\nvar results = query.TopKNear(\"Embedding\", vec, 5);","typeGuard":"static bool IsLiveQuery<T>(ILiteQueryable<T> q) => q is not null;","tryCatchPattern":null,"preventionTips":["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."],"tags":["argument-validation","vector","null-check","queryable","extension-method"],"backgroundTag":null,"analyzedSha":"f906a5f850678719e39a39a006cb66dcae563cfa","analyzedAt":"2026-08-13T21:56:30.148Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}