Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8507707
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T02:56:45+00:00 2026-06-11T02:56:45+00:00

I have two classes: Game1 and Animation. I always get that Object reference not

  • 0

I have two classes: Game1 and Animation.
I always get that “Object reference not set to an instance of an object.” error message in this line of the Game1 class: animation.Draw(spriteBatch);
What is wrong? I don’t know what to change.

Animation class code:
public class Animation 
{            
    private int _animIndex; 
    public TimeSpan PassedTime { get; private set; } 
    public Rectangle[] SourceRects { get; private set; }      
    public Texture2D Texture {get; private set; } 
    public TimeSpan Duration { get; private set; } 

    public Animation(Rectangle[] sourceRects, Texture2D texture, TimeSpan duration) 
    { 
          for (int i = 0; i < sourceRects.Length; i++) 
          { 
                sourceRects[i] = new Rectangle((sourceRects.Length - 1 - i) * (Texture.Width / sourceRects.Length), 0, Texture.Width / sourceRects.Length, Texture.Height); 
          } 

        SourceRects = sourceRects; 
        Texture = texture; 
        Duration = duration; 
    }        

    public void Update(GameTime dt) 
    { 
        PassedTime += dt.ElapsedGameTime; 
        if (PassedTime > Duration) 
        { 
            PassedTime -= Duration; // zurücksetzen 
        } 

        var percent = PassedTime.TotalSeconds / Duration.TotalSeconds; 
        _animIndex = (int)Math.Round(percent * (SourceRects.Length - 1)); 
    } 

    public void Draw(SpriteBatch batch) 
    { 
        batch.Draw(Texture, new Rectangle(0, 0, Texture.Width / SourceRects.Length, Texture.Height), SourceRects[_animIndex], Color.White); 
    } 
}

Game1 class code:
public class Game1 : Microsoft.Xna.Framework.Game 
{ 
    GraphicsDeviceManager graphics; 
    SpriteBatch spriteBatch; 
    Animation animation; 
    Texture2D gegner; 
    Rectangle[] gegnerbilder = new Rectangle[10]; 

    public Game1() 
    { 
        graphics = new GraphicsDeviceManager(this); 
        Content.RootDirectory = "Content"; 
    } 

    protected override void Initialize() 
    {          
        base.Initialize(); 
    } 

    protected override void LoadContent() 
    {            
        spriteBatch = new SpriteBatch(GraphicsDevice); 
        gegner = Content.Load<Texture2D>("kurzeanim"); 
    } 

    protected override void Update(GameTime gameTime) 
    { 
        KeyboardState kbState = Keyboard.GetState(); 
        if (kbState.IsKeyDown(Keys.A)) 
        { 
            animation = new Animation(gegnerbilder, gegner, TimeSpan.FromSeconds(3)); 
            animation.Update(gameTime); 
        } 
        base.Update(gameTime); 
    } 

    protected override void Draw(GameTime gameTime) 
    { 
        GraphicsDevice.Clear(Color.CornflowerBlue); 
        spriteBatch.Begin(); 
        animation.Draw(spriteBatch);    
        spriteBatch.End(); 

        base.Draw(gameTime); 
    } 
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T02:56:47+00:00Added an answer on June 11, 2026 at 2:56 am

    Ensure that the Update-method of the Game1-class is called before the Draw-method of that class or ensure that animation is not null before calling its members:

    protected override void Draw(GameTime gameTime) 
    { 
        if (animation != null)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue); 
            spriteBatch.Begin(); 
            animation.Draw(spriteBatch);    
            spriteBatch.End(); 
        }
        base.Draw(gameTime); 
    } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two classes Foo and Bar that Bar extends Foo as below: class
I have two classes, one that inherits from the other. The base class is
I have two classes set up as follows: class Point { protected: double coords[3];
I have two classes, and want to include a static instance of one class
I have two classes (MVC view model) which inherits from one abstract base class.
I have two classes A and B generated by cxf-codegen-plugin from my WSDL. A
I have two classes Foo and Bar . The tables in the database look
I have two classes, Item and SoldItem . SoldItem inherits from Item adding some
I have two classes, TestA and TestB . TestA extends QObject . I have
I have two classes: BaseClass : NSObject AdvanceClass : BaseClass And in AdvanceClass i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.