{"record":{"id":"fc7360c7653f7436","repo":"litedb-org/LiteDB","slug":"entity","errorCode":null,"errorMessage":"entity","messagePattern":"entity","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"LiteDB/Client/Mapper/BsonMapper.Serialize.cs","lineNumber":15,"sourceCode":"﻿using System;\nusing System.Collections;\nusing System.Linq;\nusing System.Reflection;\n\nnamespace LiteDB\n{\n    public partial class BsonMapper\n    {\n        /// <summary>\n        /// Serialize a entity class to BsonDocument\n        /// </summary>\n        public virtual BsonDocument ToDocument(Type type, object entity)\n        {\n            if (entity == null) throw new ArgumentNullException(nameof(entity));\n\n            // if object is BsonDocument, just return them\n            if (entity is BsonDocument) return (BsonDocument)(object)entity;\n\n            return this.Serialize(type, entity, 0).AsDocument;\n        }\n\n        /// <summary>\n        /// Serialize a entity class to BsonDocument\n        /// </summary>\n        public virtual BsonDocument ToDocument<T>(T entity)\n        {\n            return this.ToDocument(typeof(T), entity)?.AsDocument;\n        }\n\n        /// <summary>\n        /// Serialize to BsonValue any .NET object based on T type (using mapping rules)\n        /// </summary>","sourceCodeStart":1,"sourceCodeEnd":33,"githubUrl":"https://github.com/litedb-org/LiteDB/blob/f906a5f850678719e39a39a006cb66dcae563cfa/LiteDB/Client/Mapper/BsonMapper.Serialize.cs#L1-L33","documentation":"Thrown by BsonMapper.ToDocument(Type, object) when entity is null. The serializer must read member values from a real instance; null has no members to read. The generic ToDocument<T> delegates here.","triggerScenarios":"Calling mapper.ToDocument(typeof(Foo), null), mapper.ToDocument<MyType>(null), or passing an entity variable that was never assigned (e.g. a not-found lookup result).","commonSituations":"Inserting/upserting an object that came back null from a factory, serializing a nullable reference that was not null-checked, or processing a list that contains a null element.","solutions":["Null-check the entity before calling ToDocument.","If null is meaningful, decide whether to skip serialization or store BsonValue.Null explicitly.","Validate the source that produced the entity (repository lookup, deserialization)."],"exampleFix":"// before\nvar doc = mapper.ToDocument(entity);\n\n// after\nif (entity == null) throw new ArgumentNullException(nameof(entity));\nvar doc = mapper.ToDocument(entity);","handlingStrategy":"validation","validationCode":"if (entity == null) throw new ArgumentNullException(nameof(entity));","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Null-check entities from factories/lookups before serializing.","Filter null elements out of collections before bulk ToDocument."],"tags":["mapper","serialize","argument-null","entity"],"backgroundTag":null,"analyzedSha":"f906a5f850678719e39a39a006cb66dcae563cfa","analyzedAt":"2026-08-13T21:56:30.148Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}