{"record":{"id":"7d08e0f49693953e","repo":"litedb-org/LiteDB","slug":"value-cannot-be-null-parameter-collection-7d08e0","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/Document/BsonArray.cs","lineNumber":72,"sourceCode":"                return this.RawValue[index];\n            }\n            set\n            {\n                this.RawValue[index] = value ?? BsonValue.Null;\n            }\n        }\n\n        public int Count => this.RawValue.Count;\n\n        public bool IsReadOnly => false;\n\n        public void Add(BsonValue item) => this.RawValue.Add(item ?? BsonValue.Null);\n\n        public void AddRange<TCollection>(TCollection collection)\n            where TCollection : ICollection<BsonValue>\n        {\n            if(collection == null)\n                throw new ArgumentNullException(nameof(collection));\n\n            var list = (List<BsonValue>)base.RawValue;\n\n            var listEmptySpace = list.Capacity - list.Count;\n            if (listEmptySpace < collection.Count)\n            {\n                list.Capacity += collection.Count;\n            }\n\n            foreach (var bsonValue in collection)\n            {\n                list.Add(bsonValue ?? Null);\n            }\n        }\n        \n        public void AddRange(IEnumerable<BsonValue> items)\n        {\n            if (items == null) throw new ArgumentNullException(nameof(items));","sourceCodeStart":54,"sourceCodeEnd":90,"githubUrl":"https://github.com/litedb-org/LiteDB/blob/f906a5f850678719e39a39a006cb66dcae563cfa/LiteDB/Document/BsonArray.cs#L54-L90","documentation":"Thrown by BsonArray.AddRange<TCollection> when the collection argument is null. The method pre-sizes the backing list based on collection.Count and then enumerates, so a null collection cannot be measured or iterated. The guard prevents both a NullReferenceException and a malformed capacity calculation.","triggerScenarios":"Calling array.AddRange((List<BsonValue>)null); passing a collection from a query/lookup that returned null; feeding a nullable ICollection<BsonValue> field.","commonSituations":"Appending optional sub-collections into an existing BsonArray; merging results from conditional lookups; refactoring that left a collection uninitialized.","solutions":["Null-check the collection before AddRange; skip the call if null is acceptable.","Coalesce to an empty collection at the source.","Ensure collection-producing methods never return null."],"exampleFix":"// before\narr.AddRange(extraValues);\n\n// after\nif (extraValues is not null)\n    arr.AddRange(extraValues);","handlingStrategy":"validation","validationCode":"if (collection is not null)\n    arr.AddRange(collection);","typeGuard":"static bool IsNonNullCollection<T>(ICollection<T> c) => c is not null;","tryCatchPattern":null,"preventionTips":["Guard AddRange calls with a null check.","Coalesce optional sub-collections to empty at the source.","Ensure collection-producing methods never return null."],"tags":["argument-validation","bson","bsonarray","null-check","addrange","collection"],"backgroundTag":null,"analyzedSha":"f906a5f850678719e39a39a006cb66dcae563cfa","analyzedAt":"2026-08-13T21:56:30.148Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}