{"record":{"id":"cc74bae7e5d8422b","repo":"dotnet/wpf","slug":"value-cannot-be-null-parameter-reader","errorCode":null,"errorMessage":"Value cannot be null. (Parameter 'reader')","messagePattern":"Value cannot be null\\. \\(Parameter 'reader'\\)","errorType":"validation","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/WindowsBase/System/IO/Packaging/CompoundFile/FormatVersion.cs","lineNumber":438,"sourceCode":"#if !PBTCOMPILER\n        /// <summary>\n        /// Constructor for FormatVersion with information read from the given BinaryReader\n        /// </summary>\n        /// <param name=\"reader\">BinaryReader where version information is read 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 is preferred over the \n        /// LoadFromStream as it doesn't leave around Undisposed BinaryReader, which \n        /// LoadFromStream will\n        /// </remarks>\n        private static FormatVersion LoadFromBinaryReader(BinaryReader reader, out Int32 bytesRead)\n        {\n            checked\n            {\n                if (reader == null)\n                {\n                    throw new ArgumentNullException(nameof(reader));\n                }\n\n                FormatVersion ver = new FormatVersion();\n\n                bytesRead = 0;            // Initialize the number of bytes read\n\n                // **************\n                //  feature ID\n                // **************\n\n                Int32 strBytes;\n\n                ver._featureIdentifier = ContainerUtilities.ReadByteLengthPrefixedDWordPaddedUnicodeString(reader, out strBytes);\n                bytesRead += strBytes;\n\n                Int16 major;\n                Int16 minor;\n","sourceCodeStart":420,"sourceCodeEnd":456,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/WindowsBase/System/IO/Packaging/CompoundFile/FormatVersion.cs#L420-L456","documentation":"LoadFromBinaryReader is the internal deserializer of the compound-file FormatVersion header; it validates its BinaryReader before reading and throws ArgumentNullException (parameter 'reader') when null. It is reached via LoadFromStream, which only passes null if the caller supplied a null stream that slipped past validation.","triggerScenarios":"Indirectly, by calling FormatVersion.LoadFromStream(null, out bytesRead) on a code path where the stream null-check is bypassed; the exception surfaces at the internal LoadFromBinaryReader frame.","commonSituations":"Opening an OPC/XPS compound file from a stream that came from File.OpenRead or a response body that failed and returned null instead of a Stream.","solutions":["Ensure the Stream passed to LoadFromStream is a valid open Stream, never null","Null-check the stream source (file open result, network response) before loading","If writing a custom loader, never invoke LoadFromBinaryReader with a null reader"],"exampleFix":"// before\nvar fv = FormatVersion.LoadFromStream(GetStream(), out int read);\n// after\nStream s = GetStream();\nif (s == null) throw new InvalidOperationException(\"Source stream unavailable\");\nvar fv = FormatVersion.LoadFromStream(s, out int read);","handlingStrategy":"validation","validationCode":"if (stream == null) throw new InvalidOperationException(\"Cannot load FormatVersion: stream is null\");\nvar fv = FormatVersion.LoadFromStream(stream, out int bytesRead);","typeGuard":"static bool IsUsableStream(Stream s) => s != null && s.CanRead;","tryCatchPattern":"try { fv = FormatVersion.LoadFromStream(s, out int n); }\ncatch (ArgumentNullException ex) { log(ex.ParamName); throw new PackageOpenException(\"Invalid stream\"); }","preventionTips":["Ensure stream-producing helpers never return null; throw on failure","Check Stream.CanRead before loading compound-file headers"],"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"}