litedb-org/LiteDB · error · ArgumentNullException

id

Error message

id

What it means

ArgumentNullException thrown by LiteStorage<TFileId>.FindById when the id argument is null. The method serializes the id to a BsonValue to look up the file document, so a null id cannot be queried. The generic TFileId could be a reference type (string) allowing null, or a nullable value type.

Source

Thrown at LiteDB/Client/Storage/LiteStorage.cs:33

        private readonly ILiteDatabase _db;
        private readonly ILiteCollection<LiteFileInfo<TFileId>> _files;
        private readonly ILiteCollection<BsonDocument> _chunks;

        public LiteStorage(ILiteDatabase db, string filesCollection, string chunksCollection)
        {
            _db = db;
            _files = db.GetCollection<LiteFileInfo<TFileId>>(filesCollection);
            _chunks = db.GetCollection(chunksCollection);
        }

        #region Find Files

        /// <summary>
        /// Find a file inside datafile and returns LiteFileInfo instance. Returns null if not found
        /// </summary>
        public LiteFileInfo<TFileId> FindById(TFileId id)
        {
            if (id == null) throw new ArgumentNullException(nameof(id));

            var fileId = _db.Mapper.Serialize(typeof(TFileId), id);

            var file = _files.FindById(fileId);

            if (file == null) return null;

            file.SetReference(fileId, _files, _chunks);

            return file;
        }

        /// <summary>
        /// Find all files that match with predicate expression.
        /// </summary>
        public IEnumerable<LiteFileInfo<TFileId>> Find(BsonExpression predicate)
        {
            var query = _files.Query();

View on GitHub (pinned to f906a5f850)

Solutions

  1. Validate that id is non-null before calling FindById.
  2. Use string.IsNullOrEmpty or a null check for reference-type IDs.
  3. Return a 404/not-found result upstream if the ID is missing rather than passing null to the API.

Example fix

// before
var info = storage.FindById(requestedId); // requestedId may be null
// after
if (requestedId is null) return NotFound();
var info = storage.FindById(requestedId);
Defensive patterns

Strategy: validation

Validate before calling

if (id is null)
    return null; // or throw a domain-specific exception
return storage.FindById(id);

Type guard

static bool IsValidFileId<T>(T id) => id is not null;

Try / catch

try
{
    var info = storage.FindById(id);
}
catch (ArgumentNullException ex) when (ex.ParamName == "id")
{
    // id was null; handle the missing-ID case
}

Prevention

When it happens

Trigger: Calling storage.FindById(null) or storage.FindById(someIdVariable) where someIdVariable is null. Common when TFileId is string and the caller passed an uninitialized or user-supplied value.

Common situations: User-supplied file ID from an HTTP request that was missing. A nullable string field used as the file ID. Forgetting to validate input before querying storage.

Related errors


AI-assisted analysis of litedb-org/LiteDB@f906a5f850 (2026-08-13). Data as JSON: /api/errors/509ae9e914ae67da. Report an issue: GitHub.