{"record":{"id":"20773212c5e0e6a7","repo":"litedb-org/LiteDB","slug":"stream-207732","errorCode":null,"errorMessage":"stream","messagePattern":"stream","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"LiteDB/Client/Storage/LiteFileInfo.cs","lineNumber":67,"sourceCode":"        public LiteFileStream<TFileId> OpenRead()\n        {\n            return new LiteFileStream<TFileId>(_files, _chunks, this, _fileId, FileAccess.Read);\n        }\n\n        /// <summary>\n        /// Open file stream to write to database\n        /// </summary>\n        public LiteFileStream<TFileId> OpenWrite()\n        {\n            return new LiteFileStream<TFileId>(_files, _chunks, this, _fileId, FileAccess.Write);\n        }\n\n        /// <summary>\n        /// Copy file content to another stream\n        /// </summary>\n        public void CopyTo(Stream stream)\n        {\n            if (stream == null) throw new ArgumentNullException(nameof(stream));\n\n            using (var reader = this.OpenRead())\n            {\n                reader.CopyTo(stream);\n            }\n        }\n\n        /// <summary>\n        /// Save file content to a external file\n        /// </summary>\n        public void SaveAs(string filename, bool overwritten = true)\n        {\n            if (filename.IsNullOrWhiteSpace()) throw new ArgumentNullException(nameof(filename));\n\n            using (var file = File.Open(filename, overwritten ? FileMode.Create : FileMode.CreateNew))\n            {\n                using (var stream = this.OpenRead())\n                {","sourceCodeStart":49,"sourceCodeEnd":85,"githubUrl":"https://github.com/litedb-org/LiteDB/blob/f906a5f850678719e39a39a006cb66dcae563cfa/LiteDB/Client/Storage/LiteFileInfo.cs#L49-L85","documentation":"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.","triggerScenarios":"Calling file.CopyTo(null) or file.CopyTo(someVariableThatIsNull) where file is a LiteFileInfo<TFileId> retrieved from storage.","commonSituations":"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.","solutions":["Ensure the stream argument is non-null before calling CopyTo.","Use a null-conditional check or provide a default stream (e.g., new MemoryStream()).","If using a lazily-initialized stream, verify it is created first."],"exampleFix":"// before\nfile.CopyTo(outputStream); // outputStream may be null\n// after\nusing var outputStream = new FileStream(path, FileMode.Create);\nfile.CopyTo(outputStream);","handlingStrategy":"validation","validationCode":"if (stream is null)\n    throw new ArgumentNullException(nameof(stream));\nfile.CopyTo(stream);","typeGuard":"static bool IsValidStream(Stream s) => s is not null && s.CanWrite;","tryCatchPattern":"try\n{\n    file.CopyTo(destinationStream);\n}\ncatch (ArgumentNullException ex) when (ex.ParamName == \"stream\")\n{\n    // destinationStream was null; initialize it before calling CopyTo\n    throw;\n}","preventionTips":["Initialize the destination Stream before calling CopyTo.","Use a null check or null-coalescing operator to provide a default.","Enable nullable reference types to catch null assignments at compile time."],"tags":["argument-null","file-storage","stream","validation"],"backgroundTag":null,"analyzedSha":"f906a5f850678719e39a39a006cb66dcae563cfa","analyzedAt":"2026-08-13T21:56:30.148Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}