{"record":{"id":"854b1bf86163da35","repo":"litedb-org/LiteDB","slug":"value-cannot-be-null-parameter-dict","errorCode":null,"errorMessage":"Value cannot be null. (Parameter 'dict')","messagePattern":"Value cannot be null\\. \\(Parameter 'dict'\\)","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"LiteDB/Document/BsonDocument.cs","lineNumber":22,"sourceCode":"using System.Collections.Concurrent;\nusing System.Collections.Generic;\nusing System.Linq;\nusing System.Text;\nusing static LiteDB.Constants;\n\nnamespace LiteDB\n{\n    public class BsonDocument : BsonValue, IDictionary<string, BsonValue>\n    {\n        public BsonDocument()\n            : base(BsonType.Document, new Dictionary<string, BsonValue>(StringComparer.OrdinalIgnoreCase))\n        {\n        }\n\n        public BsonDocument(ConcurrentDictionary<string, BsonValue> dict)\n            : this()\n        {\n            if (dict == null) throw new ArgumentNullException(nameof(dict));\n\n            foreach(var element in dict)\n            {\n                this.Add(element);\n            }\n        }\n\n        public BsonDocument(IDictionary<string, BsonValue> dict)\n            : this()\n        {\n            if (dict == null) throw new ArgumentNullException(nameof(dict));\n\n            foreach (var element in dict)\n            {\n                this.Add(element);\n            }\n        }\n","sourceCodeStart":4,"sourceCodeEnd":40,"githubUrl":"https://github.com/litedb-org/LiteDB/blob/f906a5f850678719e39a39a006cb66dcae563cfa/LiteDB/Document/BsonDocument.cs#L4-L40","documentation":"Thrown when the BsonDocument constructor that accepts a ConcurrentDictionary<string, BsonValue> receives a null argument. The constructor copies elements from the supplied dictionary into the new document's internal storage, so a null source is rejected early via ArgumentNullException. This is a fail-fast guard before any iteration begins.","triggerScenarios":"Calling `new BsonDocument((ConcurrentDictionary<string, BsonValue>)null)` or passing a variable that resolved to null at runtime into the ConcurrentDictionary overload of the BsonDocument constructor.","commonSituations":"A dictionary retrieved from a cache, config, or upstream service is null because of a cache-miss or deserialization failure and is passed directly to BsonDocument without a null-check. Generic code that wraps dictionary creation and sometimes returns null.","solutions":["Check the dictionary for null before constructing the BsonDocument and either supply an empty ConcurrentDictionary or handle the missing-data case explicitly.","Use the parameterless `new BsonDocument()` when you have no initial data, then add entries individually.","Trace upstream to find why the ConcurrentDictionary is null and fix the source."],"exampleFix":"// before\nvar doc = new BsonDocument(maybeNullDict);\n// after\nvar doc = new BsonDocument(maybeNullDict ?? new ConcurrentDictionary<string, BsonValue>());","handlingStrategy":"validation","validationCode":"if (dict == null) throw new ArgumentException(\"Source dictionary is required.\");\nvar doc = new BsonDocument(dict);","typeGuard":"static bool IsValidDocumentSource(ConcurrentDictionary<string, BsonValue> dict) => dict != null;","tryCatchPattern":"try { var doc = new BsonDocument(dict); }\ncatch (ArgumentNullException ex) when (ex.ParamName == \"dict\")\n{ /* log and supply empty doc or rethrow with context */ }","preventionTips":["Always null-check dictionaries from external sources before constructing BsonDocument.","Use the parameterless constructor when the source may be absent.","Enable nullable reference types so the compiler flags potential null passes."],"tags":["bson","null-reference","constructor","argument-validation"],"backgroundTag":null,"analyzedSha":"f906a5f850678719e39a39a006cb66dcae563cfa","analyzedAt":"2026-08-13T21:56:30.148Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}