litedb-org/LiteDB · error · ArgumentNullException

command

Error message

command

What it means

Thrown by LiteDatabase.Execute(string command, BsonDocument parameters) when the SQL command string is null. The method immediately guards 'command == null' with ArgumentNullException before constructing the Tokenizer, because a null string cannot be tokenized or parsed. This is the primary entry point for raw SQL execution against the engine.

Source

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

        /// Execute SQL commands and return as data reader.
        /// </summary>
        public IBsonDataReader Execute(TextReader commandReader, BsonDocument parameters = null)
        {
            if (commandReader == null) throw new ArgumentNullException(nameof(commandReader));

            var tokenizer = new Tokenizer(commandReader);
            var sql = new SqlParser(_engine, tokenizer, parameters);
            var reader = sql.Execute();

            return reader;
        }

        /// <summary>
        /// Execute SQL commands and return as data reader
        /// </summary>
        public IBsonDataReader Execute(string command, BsonDocument parameters = null)
        {
            if (command == null) throw new ArgumentNullException(nameof(command));

            var tokenizer = new Tokenizer(command);
            var sql = new SqlParser(_engine, tokenizer, parameters);
            var reader = sql.Execute();

            return reader;
        }

        /// <summary>
        /// Execute SQL commands and return as data reader
        /// </summary>
        public IBsonDataReader Execute(string command, params BsonValue[] args)
        {
            var p = new BsonDocument();
            var index = 0;

            foreach (var arg in args)
            {

View on GitHub (pinned to f906a5f850)

Solutions

  1. Null-check the command string before calling Execute and return early or throw a domain-specific error.
  2. Use string.IsNullOrWhiteSpace to also reject blank/whitespace commands, which would otherwise fail later in the tokenizer.
  3. If the command originates from configuration, validate it at startup rather than at query time.

Example fix

// before
db.Execute(userQuery, parameters);

// after
if (string.IsNullOrWhiteSpace(userQuery))
    throw new InvalidOperationException("SQL command was not supplied.");
db.Execute(userQuery, parameters);
Defensive patterns

Strategy: validation

Validate before calling

if (string.IsNullOrWhiteSpace(command))
    throw new ArgumentException("A non-empty SQL command is required.", nameof(command));

Try / catch

try { db.Execute(command, parameters); }
catch (ArgumentNullException ex) when (ex.ParamName == "command")
{
    // log and surface a clearer domain error to the caller
}

Prevention

When it happens

Trigger: Calling db.Execute((string)null), db.Execute(null as string), or passing a variable that evaluates to null. Also reached indirectly via the Execute(string command, params BsonValue[] args) overload, which forwards a null command into this method.

Common situations: Reading SQL from a config file or environment variable that is unset/empty, passing a user-supplied query without null-checking, conditionally building a command string that stays null, or deserializing a command from JSON where the field is absent.

Related errors


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