litedb-org/LiteDB · error · ArgumentNullException

name

Error message

name

What it means

ArgumentNullException thrown by LiteDatabase.GetCollection(string name, BsonAutoId) when name is null, empty, or whitespace (checked via IsNullOrWhiteSpace). The BsonDocument collection overload requires an explicit collection name, unlike the generic overload which derives the name from typeof(T).Name.

Source

Thrown at LiteDB/Client/Database/LiteDatabase.cs:140

            return this.GetCollection<T>(null);
        }

        /// <summary>
        /// Get a collection using a name based on typeof(T).Name (BsonMapper.ResolveCollectionName function)
        /// </summary>
        public ILiteCollection<T> GetCollection<T>(BsonAutoId autoId)
        {
            return this.GetCollection<T>(null, autoId);
        }

        /// <summary>
        /// Get a collection using a generic BsonDocument. If collection does not exist, create a new one.
        /// </summary>
        /// <param name="name">Collection name (case insensitive)</param>
        /// <param name="autoId">Define autoId data type (when document contains no _id field)</param>
        public ILiteCollection<BsonDocument> GetCollection(string name, BsonAutoId autoId = BsonAutoId.ObjectId)
        {
            if (name.IsNullOrWhiteSpace()) throw new ArgumentNullException(nameof(name));

            return new LiteCollection<BsonDocument>(name, autoId, _engine, _mapper);
        }

        #endregion

        #region Transaction

        /// <summary>
        /// Initialize a new transaction. Transaction are created "per-thread". There is only one single transaction per thread.
        /// Return true if transaction was created or false if current thread already in a transaction.
        /// </summary>
        public bool BeginTrans() => _engine.BeginTrans();

        /// <summary>
        /// Commit current transaction
        /// </summary>
        public bool Commit() => _engine.Commit();

View on GitHub (pinned to f906a5f850)

Solutions

  1. Pass a non-empty collection name: db.GetCollection("customers").
  2. If the name is dynamic, validate/coalesce it before calling GetCollection.
  3. Use the generic overload db.GetCollection<T>() if you want the name derived from the entity type.

Example fix

// before
var col = db.GetCollection(name); // name is null

// after
var safeName = string.IsNullOrWhiteSpace(name) ? "default" : name.Trim();
var col = db.GetCollection(safeName);
Defensive patterns

Strategy: validation

Validate before calling

string safeName = string.IsNullOrWhiteSpace(name)
    ? throw new ArgumentException("Collection name is required.", nameof(name))
    : name.Trim();
var col = db.GetCollection(safeName);

Type guard

static bool IsValidCollectionName(string name) => !string.IsNullOrWhiteSpace(name);

Prevention

When it happens

Trigger: Calling db.GetCollection(null) or db.GetCollection("") / db.GetCollection(" ") for the BsonDocument overload; a variable-driven collection name that resolved to empty at runtime.

Common situations: Dynamic collection naming where the name source is unset; confusing the BsonDocument overload (requires name) with the generic GetCollection<T>() (name optional); trailing whitespace from input trimming.

Related errors


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