litedb-org/LiteDB · error · ArgumentNullException

Value cannot be null. (Parameter 'items')

Error message

Value cannot be null. (Parameter 'items')

What it means

Thrown by the BsonArray(IEnumerable<BsonValue>) constructor when items is null. The constructor iterates the sequence to populate the backing list, so a null enumerable cannot be enumerated. The guard ensures a clear ArgumentNullException rather than a downstream NRE.

Source

Thrown at LiteDB/Document/BsonArray.cs:35

            : this()
        {
            if (array == null) throw new ArgumentNullException(nameof(array));

            this.AddRange(array);
        }

        public BsonArray(params BsonValue[] array)
            : this()
        {
            if (array == null) throw new ArgumentNullException(nameof(array));

            this.AddRange(array);
        }

        public BsonArray(IEnumerable<BsonValue> items)
            : this()
        {
            if (items == null) throw new ArgumentNullException(nameof(items));

            this.AddRange(items);
        }
        
        public BsonArray(BsonArray items)
            : this()
        {
            if (items == null) throw new ArgumentNullException(nameof(items));

            this.AddRange(items);
        }

        public new IList<BsonValue> RawValue => (IList<BsonValue>)base.RawValue;

        public override BsonValue this[int index]
        {
            get
            {

View on GitHub (pinned to f906a5f850)

Solutions

  1. Coalesce the enumerable to Enumerable.Empty<BsonValue>() before constructing.
  2. Ensure LINQ sources are non-null.
  3. Use the parameterless constructor plus AddRange when the source is conditionally available.

Example fix

// before
var arr = new BsonArray(items);

// after
var arr = new BsonArray(items ?? Enumerable.Empty<BsonValue>());
Defensive patterns

Strategy: validation

Validate before calling

var arr = new BsonArray(items ?? Enumerable.Empty<BsonValue>());

Type guard

static bool IsNonNullEnumerable<T>(IEnumerable<T> e) => e is not null;

Prevention

When it happens

Trigger: Calling new BsonArray((IEnumerable<BsonValue>)null); passing a LINQ Where/Select result that is null due to a broken source; converting a nullable enumerable property.

Common situations: Mapping LINQ projections that may be null; optional collection fields in domain models; generics where the caller passed default(IEnumerable<BsonValue>).

Related errors


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