litedb-org/LiteDB · error · ArgumentNullException

filename

Error message

filename

What it means

ArgumentNullException thrown by LiteFileInfo<TFileId>.SaveAs when the filename argument is null, empty, or whitespace. The method uses IsNullOrWhiteSpace (a LiteDB extension) for the check, so any blank string is rejected. SaveAs opens a file on disk and writes all stored chunks to it.

Source

Thrown at LiteDB/Client/Storage/LiteFileInfo.cs:80

        /// <summary>
        /// Copy file content to another stream
        /// </summary>
        public void CopyTo(Stream stream)
        {
            if (stream == null) throw new ArgumentNullException(nameof(stream));

            using (var reader = this.OpenRead())
            {
                reader.CopyTo(stream);
            }
        }

        /// <summary>
        /// Save file content to a external file
        /// </summary>
        public void SaveAs(string filename, bool overwritten = true)
        {
            if (filename.IsNullOrWhiteSpace()) throw new ArgumentNullException(nameof(filename));

            using (var file = File.Open(filename, overwritten ? FileMode.Create : FileMode.CreateNew))
            {
                using (var stream = this.OpenRead())
                {
                    stream.CopyTo(file);
                }
            }
        }
    }
}

View on GitHub (pinned to f906a5f850)

Solutions

  1. Validate the filename is a non-empty, valid path before calling SaveAs.
  2. Use Path.Combine to build the full path from known components.
  3. Provide a fallback filename when the source is null.

Example fix

// before
file.SaveAs(filePath); // filePath may be null/empty
// after
if (!string.IsNullOrWhiteSpace(filePath))
    file.SaveAs(filePath);
else
    throw new InvalidOperationException("Export path was not configured.");
Defensive patterns

Strategy: validation

Validate before calling

if (string.IsNullOrWhiteSpace(filename))
    throw new ArgumentException("Filename is required.", nameof(filename));
file.SaveAs(filename);

Type guard

static bool IsValidFilename(string f) => !string.IsNullOrWhiteSpace(f) && f.IndexOfAny(Path.GetInvalidPathChars()) < 0;

Try / catch

try
{
    file.SaveAs(path);
}
catch (ArgumentNullException ex) when (ex.ParamName == "filename")
{
    // path was null/empty; provide a valid path
    throw;
}

Prevention

When it happens

Trigger: Calling file.SaveAs(null), file.SaveAs(""), or file.SaveAs(" "). Also triggered when filename is computed from user input or a database field that turned out to be blank.

Common situations: Reading the filename from a configuration value that was not set. Generating the path from a format string where a component was null/empty. Passing a property from a deserialized object that defaulted to null.

Related errors


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