litedb-org/LiteDB · error · ArgumentNullException
filename
Error message
filename
What it means
ArgumentNullException thrown by LiteStorage<TFileId>.Upload(TFileId id, string filename) when the filename argument is null, empty, or whitespace. This overload reads a file from disk via File.OpenRead, so a valid path is required. The check uses IsNullOrWhiteSpace.
Source
Thrown at LiteDB/Client/Storage/LiteStorage.cs:157
/// <summary>
/// Upload a file based on stream data
/// </summary>
public LiteFileInfo<TFileId> Upload(TFileId id, string filename, Stream stream, BsonDocument metadata = null)
{
using (var writer = this.OpenWrite(id, filename, metadata))
{
stream.CopyTo(writer);
return writer.FileInfo;
}
}
/// <summary>
/// Upload a file based on file system data
/// </summary>
public LiteFileInfo<TFileId> Upload(TFileId id, string filename)
{
if (filename.IsNullOrWhiteSpace()) throw new ArgumentNullException(nameof(filename));
using (var stream = File.OpenRead(filename))
{
return this.Upload(id, Path.GetFileName(filename), stream);
}
}
/// <summary>
/// Update metadata on a file. File must exist.
/// </summary>
public bool SetMetadata(TFileId id, BsonDocument metadata)
{
var file = this.FindById(id);
if (file == null) return false;
file.Metadata = metadata ?? new BsonDocument();
View on GitHub (pinned to f906a5f850)
Solutions
- Validate the filename/path is non-empty and the file exists before calling Upload.
- Use File.Exists check before the upload call.
- Return a validation error to the caller if the path is missing.
Example fix
// before
storage.Upload(id, uploadedPath); // uploadedPath may be null
// after
if (string.IsNullOrWhiteSpace(uploadedPath) || !File.Exists(uploadedPath))
throw new ArgumentException("Valid file path required.");
storage.Upload(id, uploadedPath); Defensive patterns
Strategy: validation
Validate before calling
if (string.IsNullOrWhiteSpace(filename) || !File.Exists(filename))
throw new ArgumentException("A valid file path is required.", nameof(filename));
storage.Upload(id, filename); Type guard
static bool IsValidUploadPath(string f) =>
!string.IsNullOrWhiteSpace(f) && File.Exists(f); Try / catch
try
{
storage.Upload(id, path);
}
catch (ArgumentNullException ex) when (ex.ParamName == "filename")
{
// path was null/empty; prompt the user or use a default
throw;
} Prevention
- Validate the source file path with File.Exists before calling Upload.
- Check for null/empty paths at the API boundary.
- Use Path.Combine to construct paths from validated components.
When it happens
Trigger: Calling storage.Upload(id, null), storage.Upload(id, ""), or storage.Upload(id, " "). Also when the filename comes from user input or a form upload that was not populated.
Common situations: Web API receiving a file path that the client omitted. A configuration or environment variable for the source path being unset. Path construction from nullable components.
Related errors
AI-assisted analysis of litedb-org/LiteDB@f906a5f850 (2026-08-13).
Data as JSON: /api/errors/ee34cb78f3fc9e39.
Report an issue: GitHub.