{"record":{"id":"a24370928342a886","repo":"dotnet/wpf","slug":"value-cannot-be-null-parameter-stream","errorCode":null,"errorMessage":"Value cannot be null. (Parameter 'stream')","messagePattern":"Value cannot be null\\. \\(Parameter 'stream'\\)","errorType":"validation","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/WindowsBase/System/IO/Packaging/CompoundFile/FormatVersion.cs","lineNumber":510,"sourceCode":"        }\n\n        /// <summary>\n        /// Create FormatVersion object and read version information from the given stream\n        /// </summary>\n        /// <param name=\"stream\">the stream to read version information from</param>\n        /// <param name=\"bytesRead\">number of bytes read including padding</param>\n        /// <returns>FormatVersion object</returns>\n        /// <remarks>\n        /// This operation will change the stream pointer. This function shouldn't be \n        /// used in the scenarios when LoadFromBinaryReader can do the job. \n        /// LoadFromBinaryReader will not leave around any undisposed objects, \n        /// and LoadFromStream will. \n        /// </remarks>\n        internal static FormatVersion LoadFromStream(Stream stream, out Int32 bytesRead)\n        {\n            if (stream == null)\n            {\n                throw new ArgumentNullException(nameof(stream));\n            }\n            // Suppress 56518 Local IDisposable object not disposed: \n            // Reason: The stream is not owned by the BlockManager, therefore we can \n            // close the BinaryWriter as it will Close the stream underneath.\n            BinaryReader streamReader = new BinaryReader(stream, System.Text.Encoding.Unicode);\n\n            return LoadFromBinaryReader(streamReader, out bytesRead);\n        }\n#endif\n\n        //------------------------------------------------------\n        //\n        //  Internal Events\n        //\n        //------------------------------------------------------\n        // None\n        //------------------------------------------------------\n        //","sourceCodeStart":492,"sourceCodeEnd":528,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/WindowsBase/System/IO/Packaging/CompoundFile/FormatVersion.cs#L492-L528","documentation":"LoadFromStream is the public/internal entry point that reads a compound-file FormatVersion header from a Stream. It validates the stream first and throws ArgumentNullException (parameter 'stream') when null, because it immediately wraps the stream in a Unicode BinaryReader.","triggerScenarios":"Calling FormatVersion.LoadFromStream(null, out bytesRead); typically the stream variable holds the result of a failed open or a nullable that was not checked.","commonSituations":"Opening XPS/OPC packages where the underlying file or memory stream failed to be created, e.g. File.OpenRead on a missing path combined with a null-returning helper, or deserialized settings containing a null stream handle.","solutions":["Pass a non-null, opened and readable Stream (file, MemoryStream, etc.)","Check the stream-producing call for null before invoking LoadFromStream","Wrap stream acquisition so failures throw a meaningful error instead of returning null"],"exampleFix":"// before\nvar fv = FormatVersion.LoadFromStream(stream, out int bytesRead);\n// after\nArgumentNullException.ThrowIfNull(stream);\nvar fv = FormatVersion.LoadFromStream(stream, out int bytesRead);","handlingStrategy":"validation","validationCode":"ArgumentNullException.ThrowIfNull(stream);\nif (!stream.CanRead) throw new InvalidOperationException(\"Stream is not readable\");","typeGuard":"static bool IsUsableStream(Stream s) => s != null && s.CanRead;","tryCatchPattern":"try { fv = FormatVersion.LoadFromStream(stream, out int bytesRead); }\ncatch (ArgumentNullException ex) when (ex.ParamName == \"stream\") { /* handle null stream */ }","preventionTips":["Open the stream with File.OpenRead/MemoryStream and let acquisition failures throw","Null-check nullable Stream fields before any Packaging API call"],"tags":["null-argument","argumentnullexception","stream","compound-file"],"backgroundTag":"null-argument","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}