litedb-org/LiteDB · error · ArgumentNullException

stream

Error message

stream

What it means

ArgumentNullException thrown by the LiteDatabase(Stream, ...) constructor when stream is null. The stream is the backing data store (often a MemoryStream), assigned directly to EngineSettings.DataStream; a null stream cannot back an engine so it is rejected before construction.

Source

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

        {
            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
            };

            _engine = new LiteEngine(settings);
            _mapper = mapper ?? BsonMapper.Global;
            _disposeOnClose = true;

            if (logStream == null && stream is not MemoryStream)
            {
                if (!stream.CanWrite)
                {
                    // Read-only streams cannot participate in eager checkpointing because the process
                    // writes pages back to the underlying data stream immediately.
                }
                else
                {
                    // Without a dedicated log stream the WAL lives purely in memory; force
                    // checkpointing to ensure commits reach the underlying data stream.

View on GitHub (pinned to f906a5f850)

Solutions

  1. Pass a real stream, e.g. new LiteDatabase(new MemoryStream()).
  2. Fix the stream provider to throw a descriptive error instead of returning null.
  3. Add a null check upstream of the constructor with a clear configuration message.

Example fix

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

// after
var db = new LiteDatabase(new MemoryStream());
// or
var stream = streamFactory.Create() ?? throw new InvalidOperationException("Stream factory returned null.");
var db = new LiteDatabase(stream);
Defensive patterns

Strategy: validation

Validate before calling

Stream stream = streamFactory.Create()
    ?? throw new InvalidOperationException("Stream factory returned null.");
var db = new LiteDatabase(stream);

Type guard

static bool IsUsableStream(Stream s) => s != null && (s.CanRead || s.CanWrite);

Prevention

When it happens

Trigger: Constructing new LiteDatabase((Stream)null); a factory method returning null for the stream; in-memory testing helper that forgot to allocate the MemoryStream.

Common situations: Unit test scaffolding that passes null intending to mean 'in-memory'; a stream provider returning null on error without throwing; deserialization/DI supplying null.

Related errors


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