{"record":{"id":"dd9b5add2cdc38aa","repo":"MonoGame/MonoGame","slug":"stream-dd9b5a","errorCode":null,"errorMessage":"stream","messagePattern":"stream","errorType":"validation","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"MonoGame.Framework/Graphics/Texture2D.cs","lineNumber":703,"sourceCode":"        }\n\n        /// <summary>\n        /// Creates a <see cref=\"Texture2D\"/> from a stream, supported formats bmp, gif, jpg, png, tif and dds (only for simple textures).\n        /// May work with other formats, but will not work with tga files.\n        /// </summary>\n        /// <param name=\"graphicsDevice\">The graphics device to use to create the texture.</param>\n        /// <param name=\"stream\">The stream from which to read the image data.</param>\n        /// <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>\n        /// <returns>The <see cref=\"Texture2D\"/> created from the image stream.</returns>\n        /// <remarks>Note that different image decoders may generate slight differences between platforms, but perceptually\n        /// the images should be identical.\n        /// </remarks>\n        public static Texture2D FromStream(GraphicsDevice graphicsDevice, Stream stream, Action<byte[]> colorProcessor)\n\t\t{\n            if (graphicsDevice == null)\n                throw new ArgumentNullException(\"graphicsDevice\");\n            if (stream == null)\n                throw new ArgumentNullException(\"stream\");\n\n            try\n            {\n                return PlatformFromStream(graphicsDevice, stream, colorProcessor);\n            }\n            catch(Exception e)\n            {\n                throw new InvalidOperationException(\"This image format is not supported\", e);\n            }\n        }\n\n        /// <summary>\n        /// Creates a <see cref=\"Texture2D\"/> from a stream, supported formats bmp, gif, jpg, png, tif and dds (only for simple textures).\n        /// May work with other formats, but will not work with tga files.\n        /// </summary>\n        /// <param name=\"graphicsDevice\">The graphics device to use to create the texture.</param>\n        /// <param name=\"stream\">The stream from which to read the image data.</param>\n        /// <returns>The <see cref=\"Texture2D\"/> created from the image stream.</returns>","sourceCodeStart":685,"sourceCodeEnd":721,"githubUrl":"https://github.com/MonoGame/MonoGame/blob/1d71bbd0ff4071a03caa583aa3bc56b85924a819/MonoGame.Framework/Graphics/Texture2D.cs#L685-L721","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Open and null-check the stream before passing it to FromStream.","Use File.OpenRead inside a using and verify it succeeded.","For embedded resources, confirm the resource name resolves before calling FromStream."],"exampleFix":"// before\nStream s = OpenMaybeNull();\n_tex = Texture2D.FromStream(GraphicsDevice, s); // s == null\n\n// after\nusing var s = TitleContainer.OpenStream(_assetName) ?? throw new FileNotFoundException(_assetName);\n_tex = Texture2D.FromStream(GraphicsDevice, s);","handlingStrategy":"validation","validationCode":"static Stream RequireStream(Stream s)\n{\n    if (s == null) throw new ArgumentNullException(nameof(s));\n    if (!s.CanRead) throw new ArgumentException(\"Stream is not readable.\", nameof(s));\n    if (s.CanSeek) s.Position = 0;\n    return s;\n}","typeGuard":"static bool IsReadable(Stream s) => s != null && s.CanRead;","tryCatchPattern":null,"preventionTips":["Open and null-check the stream before FromStream.","Rewind seekable streams to 0 before decoding.","Use TitleContainer.OpenStream for embedded assets and check for null."],"tags":["monogame","graphics","texture2d","fromstream","null-check","argument-null","io"],"backgroundTag":null,"analyzedSha":"1d71bbd0ff4071a03caa583aa3bc56b85924a819","analyzedAt":"2026-08-13T17:10:33.625Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}