Jump to content
  • Advertisement
Sign in to follow this  
kibokun

C# NullReferenceException I can't get rid of

This topic is 3794 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm following a nice tutorial on the basics of using the XNA library for 2d sprite gaming. The NullReferenceException comes up when I recalculate the new size of a sprite based on a float value named "scale". Here is my code for my Sprite class. This, however, is what is actually flagged in the C# debugger:
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));
            }
        }
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

namespace MyFirstGame
{
    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 a 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 sprites
        private float mScale = 1.0f;
        
        //When the scale is modified through the property
        //the size of the sprite is recalculated with the new scale applied
        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 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 its position based on the passed in speed, direction and elpased 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);
        }
    }
}

Thanks for any help. :) I'm new to C# so I have no idea how to fix this error even though from what I read, it seems common for newbies.

Share this post


Link to post
Share on other sites
Advertisement
Do a check that mSpriteTexture is not null inside set operator as:

set
{
if(mSpriteTexture != null)
{
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));
}
}


Share this post


Link to post
Share on other sites
Whenever a null reference/pointer exception comes up, mouse over all the objects in the highlighted line when in debug mode and check to see if any are currently set to null. This is usually a super easy fix with C#.

public void LoadContent(ContentManager theContentManager, string theAssetName)

Make sure theAssetName is getting the exact path, which should look something like "Sprites\\The_Sprite".

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!