stride3d/stride · error · ArgumentNullException

texture

Error message

texture

What it means

SpriteBatch.DrawSprite (invoked by the public SpriteBatch.Draw overloads) throws ArgumentNullException when the texture parameter is null. The batcher needs a valid GPU texture to record an element; null would corrupt the draw-command list. Note the public Draw overloads typically return silently for null textures — this direct path throws.

Solutions

  1. Ensure the texture is loaded/created before drawing; Content.Load the asset ahead of the draw call
  2. Skip drawing when null: if (texture != null) spriteBatch.Draw(texture, ...)
  3. Initialize textures in Start/Initialize instead of lazily in Draw
  4. Verify the texture asset exists and its reference is not broken in the scene

Example fix

// before
spriteBatch.Draw(texture, position, region, Color.White);
// after
if (texture != null)
    spriteBatch.Draw(texture, position, region, Color.White);
Defensive patterns

Strategy: type-guard

Validate before calling

if (texture == null || texture.IsDisposed) return; // skip this frame
spriteBatch.Draw(texture, destination, sourceRectangle, Color.White);

Type guard

static bool IsDrawable(Texture t) => t != null && !t.IsDisposed;

Try / catch

try { spriteBatch.Draw(texture, rect, null, Color.White); }
catch (ArgumentNullException) { Log.Warning("Draw skipped: texture not loaded"); }

Prevention

When it happens

Trigger: Calling SpriteBatch.Draw / DrawSprite with a null Texture — e.g. a Texture field never assigned, a render target not yet allocated, or content load failure.

Common situations: Sprite component whose texture reference is broken after asset rename; drawing before the render target is created; platform texture load failing silently and returning null; scene referencing a texture removed from the asset folder.

Related errors


AI-assisted analysis of stride3d/stride@96fad776d2 (2026-09-14). Data as JSON: /api/errors/4a7c099c4c1bb85a. Report an issue: GitHub.

Appendix: source

Thrown at sources/engine/Stride.Graphics/SpriteBatch.cs:520

            var drawCommand = new SpriteFont.InternalDrawCommand(this, in fontSize2, in position, in color, rotation, in origin, in scale, effects, layerDepth);

            // snap the position the closest 'real' pixel
            Vector2.Modulate(ref drawCommand.Position, ref resolutionRatio, out drawCommand.Position);
            drawCommand.Position.X = MathF.Round(drawCommand.Position.X);
            drawCommand.Position.Y = MathF.Round(drawCommand.Position.Y);
            drawCommand.Position.X /= resolutionRatio.X;
            drawCommand.Position.Y /= resolutionRatio.Y;

            spriteFont.InternalDraw(commandList, text, ref drawCommand, alignment);
        }
        
        internal unsafe void DrawSprite(Texture texture, ref RectangleF destination, bool scaleDestination, ref RectangleF? sourceRectangle, Color4 color, Color4 colorAdd,
            float rotation, ref Vector2 origin, SpriteEffects effects, ImageOrientation orientation, float depth, SwizzleMode swizzle = SwizzleMode.None, bool realSize = false)
        {
            // Check that texture is not null
            if (texture == null)
            {
                throw new ArgumentNullException("texture");
            }
            
            // Put values in next ElementInfo
            var elementInfo = new ElementInfo();
            var spriteInfo = &elementInfo.DrawInfo;

            float width;
            float height;

            // If the source rectangle has a value, then use it.
            if (sourceRectangle.HasValue)
            {
                var rectangle = sourceRectangle.Value;
                spriteInfo->Source.X = rectangle.X;
                spriteInfo->Source.Y = rectangle.Y;
                width = rectangle.Width;
                height = rectangle.Height;
            }

View on GitHub (pinned to 96fad776d2)