litedb-org/LiteDB · error · ArgumentNullException
engine
Error message
engine
What it means
ArgumentNullException thrown by the LiteDatabase(ILiteEngine, ...) constructor when engine is null. This overload wraps a pre-existing engine instance (e.g. a shared or mocked ILiteEngine); a null engine leaves LiteDatabase with nothing to delegate to, so it is rejected immediately.
Source
Thrown at LiteDB/Client/Database/LiteDatabase.cs:98
// Without a dedicated log stream the WAL lives purely in memory; force
// checkpointing to ensure commits reach the underlying data stream.
var originalCheckpointSize = _engine.Pragma(Pragmas.CHECKPOINT);
if (originalCheckpointSize != 1)
{
_engine.Pragma(Pragmas.CHECKPOINT, 1);
_checkpointOverride = originalCheckpointSize;
}
}
}
}
/// <summary>
/// Start LiteDB database using a pre-exiting engine. When LiteDatabase instance dispose engine instance will be disposed too
/// </summary>
public LiteDatabase(ILiteEngine engine, BsonMapper mapper = null, bool disposeOnClose = true)
{
_engine = engine ?? throw new ArgumentNullException(nameof(engine));
_mapper = mapper ?? BsonMapper.Global;
_disposeOnClose = disposeOnClose;
}
#endregion
#region Collections
/// <summary>
/// Get a collection using an entity class as strong typed document. 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 object contains no id field)</param>
public ILiteCollection<T> GetCollection<T>(string name, BsonAutoId autoId = BsonAutoId.ObjectId)
{
return new LiteCollection<T>(name, autoId, _engine, _mapper);
}
View on GitHub (pinned to f906a5f850)
Solutions
- Provide a real or mock ILiteEngine, e.g. new LiteDatabase(new LiteEngine(settings)).
- Register ILiteEngine in the DI container so resolution succeeds.
- Null-check the engine at the call site with a descriptive error before constructing LiteDatabase.
Example fix
// before
var db = new LiteDatabase((ILiteEngine)null);
// after
var engine = engineFactory.Create() ?? throw new InvalidOperationException("Engine factory returned null.");
var db = new LiteDatabase(engine); Defensive patterns
Strategy: validation
Validate before calling
ILiteEngine engine = engineFactory.Create()
?? throw new InvalidOperationException("Engine factory returned null; check DI registration.");
var db = new LiteDatabase(engine); Type guard
static bool HasEngine(ILiteEngine engine) => engine != null;
Prevention
- Register ILiteEngine in the DI container.
- Make engine factories throw with context instead of returning null.
- Null-check the engine at the composition root before wrapping it.
When it happens
Trigger: Constructing new LiteDatabase((ILiteEngine)null); a DI container resolving ILiteEngine to null; passing a factory result that returned null; wiring a mock engine in tests but forgetting to instantiate it.
Common situations: Dependency injection registration missing for ILiteEngine; a factory/orchestrator returning null on misconfiguration; test setup forgetting to create the mock engine.
Related errors
AI-assisted analysis of litedb-org/LiteDB@f906a5f850 (2026-08-13).
Data as JSON: /api/errors/ee0c93cf503afbce.
Report an issue: GitHub.