{"record":{"id":"6ab70e3086fcb764","repo":"litedb-org/LiteDB","slug":"doc","errorCode":null,"errorMessage":"doc","messagePattern":"doc","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"LiteDB/Client/Mapper/BsonMapper.Deserialize.cs","lineNumber":66,"sourceCode":"        private readonly HashSet<Type> _basicTypes = new HashSet<Type>\n        {\n            typeof(Int16),\n            typeof(UInt16),\n            typeof(UInt32),\n            typeof(Single),\n            typeof(Char),\n            typeof(Byte),\n            typeof(SByte)\n        };\n\n        #endregion\n\n        /// <summary>\n        /// Deserialize a BsonDocument to entity class\n        /// </summary>\n        public virtual object ToObject(Type type, BsonDocument doc)\n        {\n            if (doc == null) throw new ArgumentNullException(nameof(doc));\n\n            // if T is BsonDocument, just return them\n            if (type == typeof(BsonDocument)) return doc;\n\n            return this.Deserialize(type, doc);\n        }\n\n        /// <summary>\n        /// Deserialize a BsonDocument to entity class\n        /// </summary>\n        public virtual T ToObject<T>(BsonDocument doc)\n        {\n            return (T)this.ToObject(typeof(T), doc);\n        }\n\n        /// <summary>\n        /// Deserialize a BsonValue to .NET object typed in T\n        /// </summary>","sourceCodeStart":48,"sourceCodeEnd":84,"githubUrl":"https://github.com/litedb-org/LiteDB/blob/f906a5f850678719e39a39a006cb66dcae563cfa/LiteDB/Client/Mapper/BsonMapper.Deserialize.cs#L48-L84","documentation":"Thrown by BsonMapper.ToObject(Type, BsonDocument) when doc is null. The mapper needs a source document to read field values from; null has nothing to deserialize. The generic ToObject<T> delegates here.","triggerScenarios":"Calling mapper.ToObject<MyType>(null), mapper.ToObject(typeof(Foo), null), or passing a BsonDocument fetched via a lookup that returned null (e.g. FindById with a missing key).","commonSituations":"Deserializing a document that does not exist (FindById returned null), mapping a nullable relationship, or processing a pipeline where an earlier step yields null.","solutions":["Null-check the document before calling ToObject and handle the missing case explicitly.","If null is a valid input, return default(T) instead of calling the mapper.","Check the upstream Find/FindById call that produced the document."],"exampleFix":"// before\nvar entity = mapper.ToObject<MyEntity>(doc);\n\n// after\nif (doc == null) return null;\nvar entity = mapper.ToObject<MyEntity>(doc);","handlingStrategy":"validation","validationCode":"if (doc == null) return default; // or throw a domain-specific error","typeGuard":"static bool HasDocument(BsonDocument doc) => doc != null;","tryCatchPattern":null,"preventionTips":["Null-check FindById/Find results before mapping.","Treat a null document as 'not found' at the repository layer."],"tags":["mapper","deserialize","argument-null","bsondocument"],"backgroundTag":null,"analyzedSha":"f906a5f850678719e39a39a006cb66dcae563cfa","analyzedAt":"2026-08-13T21:56:30.148Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}