{"record":{"id":"7e31d41e6cf12baf","repo":"stride3d/stride","slug":"spritebatch","errorCode":null,"errorMessage":"spriteBatch","messagePattern":"spriteBatch","errorType":"validation","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"sources/engine/Stride.Graphics/SpriteExtensions.cs","lineNumber":49,"sourceCode":"\n        /// <summary>\n        /// Draw a sprite using a sprite batch.\n        /// </summary>\n        /// <param name=\"sprite\">The sprite</param>\n        /// <param name=\"spriteBatch\">The sprite batch used to draw the sprite.</param>\n        /// <param name=\"position\">The position to which draw the sprite</param>\n        /// <param name=\"color\">The color to use to draw the sprite</param>\n        /// <param name=\"rotation\">The rotation to apply on the sprite</param>\n        /// <param name=\"scales\">The scale factors to apply on the sprite</param>\n        /// <param name=\"depthLayer\">The depth layer to which draw the sprite</param>\n        /// <param name=\"spriteEffects\">The sprite effect to apply on the sprite</param>\n        /// <remarks>This function must be called between the <see cref=\"SpriteBatch.Begin\"/>\n        /// and `SpriteBatch.End()` calls of the provided <paramref name=\"spriteBatch\"/></remarks>\n        /// <exception cref=\"ArgumentException\">The provided frame index is not valid.</exception>\n        /// <exception cref=\"ArgumentOutOfRangeException\">The provided spriteBatch is null</exception>\n        public static void Draw(this Sprite sprite, SpriteBatch spriteBatch, Vector2 position, Color color, Vector2 scales, float rotation = 0f, float depthLayer = 0, SpriteEffects spriteEffects = SpriteEffects.None)\n        {\n            if (spriteBatch == null) throw new ArgumentNullException(\"spriteBatch\");\n\n            if (sprite.Texture == null)\n                return;\n\n            spriteBatch.Draw(sprite.Texture, position, sprite.Region, color, rotation, sprite.Center, scales, spriteEffects, sprite.Orientation, depthLayer);\n        }\n\n        /// <summary>\n        /// Draw a sprite in the 3D world using the provided 3D sprite batch, world matrix and color.\n        /// </summary>\n        /// <param name=\"sprite\">The sprite</param>\n        /// <param name=\"spriteBatch\">The sprite batch used to draw the sprite.</param>\n        /// <param name=\"worldMatrix\">The world matrix of the sprite</param>\n        /// <param name=\"color\">The color to apply on the sprite</param>\n        /// <remarks>This function must be called between the <see cref=\"SpriteBatch.Begin\"/>\n        /// and `SpriteBatch.End()` calls of the provided <paramref name=\"spriteBatch\"/></remarks>\n        /// <exception cref=\"ArgumentException\">The provided frame index is not valid.</exception>\n        /// <exception cref=\"ArgumentOutOfRangeException\">The provided spriteBatch is null</exception>","sourceCodeStart":31,"sourceCodeEnd":67,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/engine/Stride.Graphics/SpriteExtensions.cs#L31-L67","documentation":"The Sprite.Draw extension method throws ArgumentNullException when the provided spriteBatch is null (the XML docs even label it ArgumentOutOfRangeException, but the code throws ArgumentNullException). The sprite needs a batch to record its draw command; without one nothing can be rendered.","triggerScenarios":"Calling sprite.Draw(null, position, ...) — usually a SpriteBatch field that was not resolved from a service/scene, or drawing before the render system created the batch.","commonSituations":"Forgetting to inject/resolve the SpriteBatch from the game's Services; calling Draw in a component before base.Initialize created the batch; ordering issue where the UI system is torn down before a late draw call.","solutions":["Pass a valid SpriteBatch instance (typically resolved via Services.GetService<SpriteBatch>() or the render system's batch)","Guard the call: if (spriteBatch != null) sprite.Draw(spriteBatch, ...)","Move the draw call after batch initialization (e.g. out of LoadContent into Draw phase)","Check component initialization order so the batch exists before the sprite draws"],"exampleFix":"// before\nsprite.Draw(spriteBatch, Position); // spriteBatch may be null\n// after\nspriteBatch = Services.GetSafeServiceAs<SpriteBatch>();\nif (spriteBatch != null)\n    sprite.Draw(spriteBatch, Position);","handlingStrategy":"type-guard","validationCode":"if (spriteBatch == null) spriteBatch = Services.GetSafeServiceAs<SpriteBatch>();\nif (spriteBatch != null) sprite.Draw(spriteBatch, position);","typeGuard":"static bool CanDraw(Sprite sprite, SpriteBatch batch) => batch != null && sprite?.Texture != null;","tryCatchPattern":"try { sprite.Draw(spriteBatch, pos); }\ncatch (ArgumentNullException) { Log.Warning(\"Sprite draw skipped: no SpriteBatch resolved\"); }","preventionTips":["Resolve SpriteBatch via Services in Initialize, cache it in a field","Draw only during the render phase after the graphics system is initialized","Dispose/teardown order: stop drawing before the batch is disposed"],"tags":["csharp","null-argument","graphics","sprite"],"backgroundTag":"null-argument","analyzedSha":"96fad776d210c221682aac1ccdf4c79dc046fc38","analyzedAt":"2026-09-14T02:59:31.279Z","contentChangedAt":"2026-09-14T02:59:31.279Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}