litedb-org/LiteDB · error · ArgumentNullException

stream

Error message

stream

What it means

Standard ArgumentNullException thrown by LiteFileInfo<TFileId>.CopyTo when the destination stream argument is null. CopyTo opens an internal read stream and copies all file chunks into the provided Stream, so a null destination is a programming error.

Source

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

        public LiteFileStream<TFileId> OpenRead()
        {
            return new LiteFileStream<TFileId>(_files, _chunks, this, _fileId, FileAccess.Read);
        }

        /// <summary>
        /// Open file stream to write to database
        /// </summary>
        public LiteFileStream<TFileId> OpenWrite()
        {
            return new LiteFileStream<TFileId>(_files, _chunks, this, _fileId, FileAccess.Write);
        }

        /// <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())
                {

View on GitHub (pinned to f906a5f850)

Solutions

  1. Ensure the stream argument is non-null before calling CopyTo.
  2. Use a null-conditional check or provide a default stream (e.g., new MemoryStream()).
  3. If using a lazily-initialized stream, verify it is created first.

Example fix

// before
file.CopyTo(outputStream); // outputStream may be null
// after
using var outputStream = new FileStream(path, FileMode.Create);
file.CopyTo(outputStream);
Defensive patterns

Strategy: validation

Validate before calling

if (stream is null)
    throw new ArgumentNullException(nameof(stream));
file.CopyTo(stream);

Type guard

static bool IsValidStream(Stream s) => s is not null && s.CanWrite;

Try / catch

try
{
    file.CopyTo(destinationStream);
}
catch (ArgumentNullException ex) when (ex.ParamName == "stream")
{
    // destinationStream was null; initialize it before calling CopyTo
    throw;
}

Prevention

When it happens

Trigger: Calling file.CopyTo(null) or file.CopyTo(someVariableThatIsNull) where file is a LiteFileInfo<TFileId> retrieved from storage.

Common situations: Passing a MemoryStream or FileStream variable that was conditionally initialized and ended up null. Forgetting to create the output stream before calling CopyTo. Passing a field/property that hasn't been assigned.

Related errors


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