MonoGame/MonoGame · error · ArgumentNullException

stream

Error message

stream

What it means

Thrown by Texture2D.FromStream as an ArgumentNullException when the stream argument is null. FromStream reads and decodes image bytes from the supplied Stream, so a null stream has nothing to read.

Source

Thrown at MonoGame.Framework/Graphics/Texture2D.cs:703

        }

        /// <summary>
        /// Creates a <see cref="Texture2D"/> from a stream, supported formats bmp, gif, jpg, png, tif and dds (only for simple textures).
        /// May work with other formats, but will not work with tga files.
        /// </summary>
        /// <param name="graphicsDevice">The graphics device to use to create the texture.</param>
        /// <param name="stream">The stream from which to read the image data.</param>
        /// <param name="colorProcessor">Function that is applied to the data in RGBA format before the texture is sent to video memory. Could be null(no processing then).</param>
        /// <returns>The <see cref="Texture2D"/> created from the image stream.</returns>
        /// <remarks>Note that different image decoders may generate slight differences between platforms, but perceptually
        /// the images should be identical.
        /// </remarks>
        public static Texture2D FromStream(GraphicsDevice graphicsDevice, Stream stream, Action<byte[]> colorProcessor)
		{
            if (graphicsDevice == null)
                throw new ArgumentNullException("graphicsDevice");
            if (stream == null)
                throw new ArgumentNullException("stream");

            try
            {
                return PlatformFromStream(graphicsDevice, stream, colorProcessor);
            }
            catch(Exception e)
            {
                throw new InvalidOperationException("This image format is not supported", e);
            }
        }

        /// <summary>
        /// Creates a <see cref="Texture2D"/> from a stream, supported formats bmp, gif, jpg, png, tif and dds (only for simple textures).
        /// May work with other formats, but will not work with tga files.
        /// </summary>
        /// <param name="graphicsDevice">The graphics device to use to create the texture.</param>
        /// <param name="stream">The stream from which to read the image data.</param>
        /// <returns>The <see cref="Texture2D"/> created from the image stream.</returns>

View on GitHub (pinned to 1d71bbd0ff)

Solutions

  1. Open and null-check the stream before passing it to FromStream.
  2. Use File.OpenRead inside a using and verify it succeeded.
  3. For embedded resources, confirm the resource name resolves before calling FromStream.

Example fix

// before
Stream s = OpenMaybeNull();
_tex = Texture2D.FromStream(GraphicsDevice, s); // s == null

// after
using var s = TitleContainer.OpenStream(_assetName) ?? throw new FileNotFoundException(_assetName);
_tex = Texture2D.FromStream(GraphicsDevice, s);
Defensive patterns

Strategy: validation

Validate before calling

static Stream RequireStream(Stream s)
{
    if (s == null) throw new ArgumentNullException(nameof(s));
    if (!s.CanRead) throw new ArgumentException("Stream is not readable.", nameof(s));
    if (s.CanSeek) s.Position = 0;
    return s;
}

Type guard

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

Prevention

When it happens

Trigger: Calling Texture2D.FromStream(device, null); passing a MemoryStream/FileStream that failed to open and was left null; loading from a network or resource stream that returned null.

Common situations: File-open wrapped in try/catch that swallows the error and leaves stream null; embedded resource lookup miss returning null; refactoring that drops the stream creation.

Related errors


AI-assisted analysis of MonoGame/MonoGame@1d71bbd0ff (2026-08-13). Data as JSON: /api/errors/dd9b5add2cdc38aa. Report an issue: GitHub.