Sunday, June 30, 2013

Visual Studio throws an exception: "Object reference not set to an instance of an object."

I'm working in C# (actually XNA) and I have this code for my "Sprite.cs" class:



#region Using Statements
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
#endregion

#region Sprite Logic
namespace XNAGame
{
class Sprite
{
//The current position of the Sprite
public Vector2 Position = new Vector2(0, 0);

//The texture object used when drawing the sprite
private Texture2D mSpriteTexture;

//The asset name for the Sprite's Texture
public string AssetName;

//The Size of the Sprite (with scale applied)
public Rectangle Size;

//The amount to increase/decrease the size of the original sprite. When
//modified throught he property, the Size of the sprite is recalculated
//with the new scale applied.
private float mScale = 1.0f;
public float Scale
{
get { return mScale; }
set
{
mScale = value;
//Recalculate the Size of the Sprite with the new scale
Size = new Rectangle(0, 0, (int)(mSpriteTexture.Width * Scale), (int)(mSpriteTexture.Height * Scale));
}
}

//Load the texture for the sprite using the Content Pipeline
public void LoadContent(ContentManager theContentManager, string theAssetName)
{
mSpriteTexture = theContentManager.Load<Texture2D>(theAssetName);
AssetName = theAssetName;
Size = new Rectangle(0, 0, (int)(mSpriteTexture.Width * Scale), (int)(mSpriteTexture.Height * Scale));
}

//Update the Sprite and change it's position based on the passed in speed, direction and elapsed time.
public void Update(GameTime theGameTime, Vector2 theSpeed, Vector2 theDirection)
{
Position += theDirection * theSpeed * (float)theGameTime.ElapsedGameTime.TotalSeconds;
}

//Draw the sprite to the screen
public void Draw(SpriteBatch theSpriteBatch)
{
theSpriteBatch.Draw(mSpriteTexture, Position,
new Rectangle(0, 0, mSpriteTexture.Width, mSpriteTexture.Height),
Color.White, 0.0f, Vector2.Zero, Scale, SpriteEffects.None, 0);
}

}
}
#endregion



When I start debugging I get this exception: "Object reference not set to an instance of an object."




I LOVE VISUAL STUDIO ULTIMATE 2012!!! (Since I was coding with the express versions, now when I have the brand new one it's great!


No comments:

Post a Comment