dotnet/wpf · error · ArgumentNullException

Value cannot be null. (Parameter 'stream')

Error message

Value cannot be null. (Parameter 'stream')

What it means

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.

Solutions

  1. Pass a non-null, opened and readable Stream (file, MemoryStream, etc.)
  2. Check the stream-producing call for null before invoking LoadFromStream
  3. Wrap stream acquisition so failures throw a meaningful error instead of returning null

Example fix

// before
var fv = FormatVersion.LoadFromStream(stream, out int bytesRead);
// after
ArgumentNullException.ThrowIfNull(stream);
var fv = FormatVersion.LoadFromStream(stream, out int bytesRead);
Defensive patterns

Strategy: validation

Validate before calling

ArgumentNullException.ThrowIfNull(stream);
if (!stream.CanRead) throw new InvalidOperationException("Stream is not readable");

Type guard

static bool IsUsableStream(Stream s) => s != null && s.CanRead;

Try / catch

try { fv = FormatVersion.LoadFromStream(stream, out int bytesRead); }
catch (ArgumentNullException ex) when (ex.ParamName == "stream") { /* handle null stream */ }

Prevention

When it happens

Trigger: Calling FormatVersion.LoadFromStream(null, out bytesRead); typically the stream variable holds the result of a failed open or a nullable that was not checked.

Common situations: 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.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/a24370928342a886. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/WindowsBase/System/IO/Packaging/CompoundFile/FormatVersion.cs:510

        }

        /// <summary>
        /// Create FormatVersion object and read version information from the given stream
        /// </summary>
        /// <param name="stream">the stream to read version information from</param>
        /// <param name="bytesRead">number of bytes read including padding</param>
        /// <returns>FormatVersion object</returns>
        /// <remarks>
        /// This operation will change the stream pointer. This function shouldn't be 
        /// used in the scenarios when LoadFromBinaryReader can do the job. 
        /// LoadFromBinaryReader will not leave around any undisposed objects, 
        /// and LoadFromStream will. 
        /// </remarks>
        internal static FormatVersion LoadFromStream(Stream stream, out Int32 bytesRead)
        {
            if (stream == null)
            {
                throw new ArgumentNullException(nameof(stream));
            }
            // Suppress 56518 Local IDisposable object not disposed: 
            // Reason: The stream is not owned by the BlockManager, therefore we can 
            // close the BinaryWriter as it will Close the stream underneath.
            BinaryReader streamReader = new BinaryReader(stream, System.Text.Encoding.Unicode);

            return LoadFromBinaryReader(streamReader, out bytesRead);
        }
#endif

        //------------------------------------------------------
        //
        //  Internal Events
        //
        //------------------------------------------------------
        // None
        //------------------------------------------------------
        //

View on GitHub (pinned to 81131a70a4)