litedb-org/LiteDB · error · ArgumentNullException
commandReader
Error message
commandReader
What it means
ArgumentNullException thrown by LiteDatabase.Execute(TextReader commandReader, BsonDocument parameters) when commandReader is null. The overload builds a Tokenizer over the reader and then a SqlParser, so a null reader cannot be tokenized and is rejected up front.
Source
Thrown at LiteDB/Client/Database/LiteDatabase.cs:247
/// </summary>
public bool RenameCollection(string oldName, string newName)
{
if (oldName.IsNullOrWhiteSpace()) throw new ArgumentNullException(nameof(oldName));
if (newName.IsNullOrWhiteSpace()) throw new ArgumentNullException(nameof(newName));
return _engine.RenameCollection(oldName, newName);
}
#endregion
#region Execute SQL
/// <summary>
/// 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();View on GitHub (pinned to f906a5f850)
Solutions
- Pass a valid TextReader, e.g. db.Execute(new StringReader(sql)) or db.Execute(new StreamReader(file)).
- If you have a plain string, use the string overload db.Execute(sql).
- Null-check the reader source (file/stream) and fail with a clear message before calling Execute.
Example fix
// before
db.Execute((TextReader)null);
// after
db.Execute(new StringReader("SELECT * FROM customers"));
// or for a string
db.Execute("SELECT * FROM customers"); Defensive patterns
Strategy: validation
Validate before calling
if (commandReader == null) throw new ArgumentNullException(nameof(commandReader)); var reader = db.Execute(commandReader);
Type guard
static bool IsUsableReader(TextReader r) => r != null;
Prevention
- Null-check the reader (and its underlying stream/file) before calling Execute.
- Use the string overload db.Execute(sql) for plain SQL strings.
- Make file/stream openers throw descriptive errors instead of returning null.
When it happens
Trigger: Calling db.Execute((TextReader)null); a stream/file that failed to open being passed as null; a multi-statement execution path where the reader was not constructed.
Common situations: File-open failure returning null instead of throwing; helper that builds a reader from a possibly-null stream; confusing the TextReader overload with the string overload and passing null.
Related errors
AI-assisted analysis of litedb-org/LiteDB@f906a5f850 (2026-08-13).
Data as JSON: /api/errors/3b395098b1f89be3.
Report an issue: GitHub.