0

The exactly same line of code is used couple of lines below and it works just fine. My teacher couldn't figure it out. I use XNA in visual basic C#. When I run my game error message "NullReferenceException was unhandled", "Object reference not set to an instance of an object." is displayed.

Line that trows the error:

Size = new Rectangle(0, 0, (int)(mBallTexture.Width * Scale),(int)(mBallTexture.Height * Scale));

My code:

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

            //The size of the Sprite
            public Rectangle Size;

            //Used to size the Sprite up or down from the original image
            public float mScale = 1.0f;

            //The current position of the Sprite
            public Vector2 Position = new Vector2(500, 0);

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




            //When the scale is modified throught he 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


    //THIS CODE LINE IS THE ONE CAUSING TROUBLE
    Size = new Rectangle(0, 0, (int)(mBallTexture.Width * Scale), (int)(mBallTexture.Height * Scale));

                    ////Size = new Rectangle(0, 0, 50, 50);
                }
            }


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

0 Answers0