litedb-org/LiteDB · error · ArgumentNullException

connectionString

Error message

connectionString

What it means

ArgumentNullException thrown by the LiteDatabase(ConnectionString, BsonMapper) constructor when connectionString is null. The constructor immediately delegates to connectionString.CreateEngine(), so a null connection string cannot build an engine and is rejected before any I/O.

Source

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

        #endregion

        #region Ctor

        /// <summary>
        /// Starts LiteDB database using a connection string for file system database
        /// </summary>
        public LiteDatabase(string connectionString, BsonMapper mapper = null)
            : this(new ConnectionString(connectionString), mapper)
        {
        }

        /// <summary>
        /// Starts LiteDB database using a connection string for file system database
        /// </summary>
        public LiteDatabase(ConnectionString connectionString, BsonMapper mapper = null)
        {
            if (connectionString == null) throw new ArgumentNullException(nameof(connectionString));

            _engine = connectionString.CreateEngine();
            _mapper = mapper ?? BsonMapper.Global;
            _disposeOnClose = true;
        }

        /// <summary>
        /// Starts LiteDB database using a generic Stream implementation (mostly MemoryStream).
        /// </summary>
        /// <param name="stream">DataStream reference </param>
        /// <param name="mapper">BsonMapper mapper reference</param>
        /// <param name="logStream">LogStream reference </param>
        public LiteDatabase(Stream stream, BsonMapper mapper = null, Stream logStream = null)
        {
            var settings = new EngineSettings
            {
                DataStream = stream ?? throw new ArgumentNullException(nameof(stream)),
                LogStream = logStream

View on GitHub (pinned to f906a5f850)

Solutions

  1. Provide a valid connection string, e.g. new LiteDatabase(new ConnectionString("MyData.db")).
  2. Load the value from configuration and fail fast with a clear message if it is missing.
  3. Validate the connection string is non-null before constructing LiteDatabase.

Example fix

// before
var db = new LiteDatabase((ConnectionString)null);

// after
var cs = Configuration.GetConnectionString("LiteDB")
    ?? throw new InvalidOperationException("LiteDB connection string is not configured.");
var db = new LiteDatabase(new ConnectionString(cs));
Defensive patterns

Strategy: validation

Validate before calling

var cs = Configuration.GetConnectionString("LiteDB")
    ?? throw new InvalidOperationException("LiteDB connection string is missing in configuration.");
var db = new LiteDatabase(new ConnectionString(cs));

Type guard

static bool IsValidConnectionString(string s) => !string.IsNullOrWhiteSpace(s);

Prevention

When it happens

Trigger: Constructing new LiteDatabase((ConnectionString)null) or new LiteDatabase((string)null) (the string overload forwards to this one); a configuration system returning null for the connection string; a dependency-injection registration missing the connection string value.

Common situations: Missing/empty 'ConnectionStrings' app setting; environment variable not set in the deployment; test helper passing null; typo in the config key name.

Related errors


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