1 Replies - 638 Views - Last Post: 22 September 2012 - 07:19 AM

#1 Reapter  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 21-September 12

Rearranged XNA causing Spritebatch to give NullException.

Posted 21 September 2012 - 03:21 PM

Alright, so I am basically rewriting my code I had run through once fully for a menu so it is easier to manage. But for some where it doesn't appear to be loading my 2D_Texture correctly and I am having trouble figuring out how to go about this or where in my order of things is causing the problem. I tried rearranging it quite a few times but couldn't seem to get it to work out with my current setup although I had fixed any other issues I have run into. Thank you.

The Error specifically is ArgumentNullException on the texture of my Main_Menu_Spritebatch which is probably the result of my order that things are loading specifically in my Main_Menu_Graphics class possibly with my content manager. Thank you.

//Preprocessor Directives
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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 for ease of use.
namespace Xna_Menu_Cleaned_Up_2
{
    //Main Menu Graphics Master Class
    class Main_Menu_Graphics
    {
        //Global Variable Class Call.
        Global_Variables_And_Initilizations Global_Variable_And_Init = new Global_Variables_And_Initilizations();
    
        //Spritebatch
        SpriteBatch Main_Menu_Spritebatch = new SpriteBatch(Global_Variables_And_Initilizations.Master_Global_Graphics_Device);

        //2D Textures For Use.
        Texture2D Texture_Button_Textbox;

        //Graphic Positions
        Vector2 Position_Button_Textbox = Vector2.Zero;

        //Main Menu Load Function
        public void Main_Menu_Graphics_Load()
        {
            //Load Graphics to Textures.
            Texture_Button_Textbox = Global_Variables_And_Initilizations.Master_Global_Content_Manager.Load<Texture2D>("Main_Menu/2D_Graphics/Main_Menu_Textbox");            
        }

        //Main Menu Graphics Function
        public void Main_Menu_Graphics_Draw()
        {           
            //Clear Screen to Teal.
            Global_Variables_And_Initilizations.Master_Global_Graphics_Device.Clear(Color.Teal);
                        
            Main_Menu_Spritebatch.Begin();
            Main_Menu_Spritebatch.Draw(Texture_Button_Textbox, Position_Button_Textbox, Color.White);
            Main_Menu_Spritebatch.End();
        }
    }
}



//Preprocessor Directives
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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 for ease of use.
namespace Xna_Menu_Cleaned_Up_2
{
    //Class for global variables and inits.
    class Global_Variables_And_Initilizations
    {
        //Enable the main menu at all.
        public static int Main_Menu_Enable = 1;

        //Value for Graphics device manager for all classes.
        public static GraphicsDeviceManager Master_Global_Graphics_Device_Manager;

        //Value for all Content to be managed.
        public static ContentManager Master_Global_Content_Manager;     

        //Value for all Graphics Devices.
        public static GraphicsDevice Master_Global_Graphics_Device;
    }
}



//Preprocessor Directives
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 for ease of use.
namespace Xna_Menu_Cleaned_Up_2
{
    //Core program class.
    public class Program_Core : Microsoft.Xna.Framework.Game
    {
        //Graphics Device Pass
        GraphicsDeviceManager Master_Graphice_Device_Manager_Passthrough;
       
        //Program core main.
        public Program_Core()
        {
            //Init the device and passthrough for use.
            Master_Graphice_Device_Manager_Passthrough = new GraphicsDeviceManager(this);       

            //Points to content directory.
            Content.RootDirectory = "Content"; 
        }

        //Init Method
        protected override void Initialize()
        {
            //Define Global Content Manager
            Global_Variables_And_Initilizations.Master_Global_Content_Manager = this.Content;
            
            //Define Global Graphics Device
            Global_Variables_And_Initilizations.Master_Global_Graphics_Device = GraphicsDevice;
            
            //Define Global Graphics Device Manager
            Global_Variables_And_Initilizations.Master_Global_Graphics_Device_Manager = Master_Graphice_Device_Manager_Passthrough;
        
            //Base init call.
            base.Initialize();
        }

        //Load Content Method
        protected override void LoadContent()
        {
            //Define Menu Class For Use.
            Main_Menu_Graphics Main_Menu_Call = new Main_Menu_Graphics();

            //Main Menu Load Content Call
            Main_Menu_Call.Main_Menu_Graphics_Load();
        }

        //Unload Content Method
        protected override void UnloadContent()
        {

        }

        //Update Method and Game Time
        protected override void Update(GameTime gameTime)
        {

            //Update base call.
            base.Update(gameTime);
        }

        //Draw Method and Game Time
        protected override void Draw(GameTime gameTime)
        {
            //Define Menu Class For Use.
            Main_Menu_Graphics Main_Menu_Call = new Main_Menu_Graphics();

            //Main Menu Draw
            Main_Menu_Call.Main_Menu_Graphics_Draw();
            
            //Draw Base call.
            base.Draw(gameTime);
        }
    }
}



Is This A Good Question/Topic? 0
  • +

Replies To: Rearranged XNA causing Spritebatch to give NullException.

#2 Reapter  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 21-September 12

Re: Rearranged XNA causing Spritebatch to give NullException.

Posted 22 September 2012 - 07:19 AM

Just an update after searching through my other code and trying a few more things. The solution was to just add static behind the texture name in case anyone else runs into this issue.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1