0

I have three classes, the main called Game1.cs, and two other classes inside a folder called Images.cs and Resize.cs. In Images the images are loaded in and are able to be used. In Resize I want to draw an integer called size that is equal to 1. I am getting a null value error and don't know why.

If I try to place size any were in Game1.cs it will bring up the same error.

Game1.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace **
{
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        public static SpriteFont font1;

        private StartUp.Resize resize;

        static new Rectangle con = new Rectangle(330, 246, 364, 84);
        static new Rectangle cre = new Rectangle(330, 430, 364, 84);
        static new Rectangle exit = new Rectangle(330, 522, 364, 84);
        static new Rectangle logo = new Rectangle(216, 37, 591, 71);
        static new Rectangle opt = new Rectangle(330, 338, 364, 84);
        static new Rectangle play = new Rectangle(330, 154, 364, 84);
        static new Rectangle ver = new Rectangle(7, 701, 215, 39);

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

            graphics.PreferredBackBufferHeight = 768;
            graphics.PreferredBackBufferWidth = 1024;
            graphics.ApplyChanges();

            StartUp.Images.Content = Content;
        }

        protected override void Initialize()
        {
            this.IsMouseVisible = true;
            base.Initialize();
        }

        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(GraphicsDevice);

            StartUp.Images.Load();
            font1 = Content.Load<SpriteFont>(@"Fonts\Font1");
        }

        protected override void UnloadContent()
        {
        }

        protected override void Update(GameTime gameTime)
        {
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            resize.size = 1;

            base.Update(gameTime);
        }

        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            spriteBatch.Begin(SpriteSortMode.FrontToBack, BlendState.AlphaBlend, SamplerState.PointClamp, DepthStencilState.Default, RasterizerState.CullCounterClockwise);
            spriteBatch.Draw(StartUp.Images.Continue, con, Color.White);
            spriteBatch.Draw(StartUp.Images.Credits, cre, Color.White);
            spriteBatch.Draw(StartUp.Images.Exit, exit, Color.White);
            spriteBatch.Draw(StartUp.Images.Play, play, Color.White);
            spriteBatch.Draw(StartUp.Images.Options, opt, Color.White);
            spriteBatch.Draw(StartUp.Images.Logo, logo, Color.White);
            spriteBatch.Draw(StartUp.Images.Version, ver, Color.White);
//error here 
            spriteBatch.DrawString(font1, resize.size.ToString(), new Vector2(0, 0), Color.White);
//error here
            spriteBatch.End();

            base.Draw(gameTime);
        }
    }
}

Resize.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace **.StartUp
{
    public class Resize
    {
        public int size = 1;

        public int continueWidth;
        public int continueHeight;

        public int ContinueWidth;
        public int ContinueHeight;
    }
}
Ryan Foy
  • 322
  • 6
  • 19

1 Answers1

0

That's because you haven't initialized it before using it.

In your Game1 constructor initialize it:

public Game1()
{
    ...

    resize = new Resize();
}
gleng
  • 6,185
  • 4
  • 21
  • 35