litedb-org/LiteDB · error · ArgumentNullException

Value cannot be null. (Parameter 'array')

Error message

Value cannot be null. (Parameter 'array')

What it means

Thrown by the BsonArray(List<BsonValue>) constructor when the supplied list is null. The constructor copies the list contents into the array's backing storage, so a null source has nothing to enumerate. The guard fires before AddRange is attempted.

Source

Thrown at LiteDB/Document/BsonArray.cs:19

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using static LiteDB.Constants;

namespace LiteDB
{
    public class BsonArray : BsonValue, IList<BsonValue>
    {
        public BsonArray()
            : base(BsonType.Array, new List<BsonValue>())
        {
        }

        public BsonArray(List<BsonValue> array)
            : 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);

View on GitHub (pinned to f906a5f850)

Solutions

  1. Pass a non-null list or use the parameterless constructor followed by AddRange.
  2. Coalesce null lists to an empty list at the source.
  3. Guard collection-producing logic to never return null.

Example fix

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

// after
var arr = new BsonArray(myList ?? new List<BsonValue>());
Defensive patterns

Strategy: validation

Validate before calling

var arr = new BsonArray(list ?? new List<BsonValue>());

Type guard

static bool IsNonNullList(List<BsonValue> l) => l is not null;

Prevention

When it happens

Trigger: Calling new BsonArray((List<BsonValue>)null); passing a list from a LINQ query that returned null; converting a nullable list field directly into the constructor.

Common situations: Mapping nullable collection properties from DTOs into BsonArray; deserializing optional array fields that were absent; refactoring that left a list uninitialized.

Related errors


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