• Advertisement
Sign in to follow this  

Object Reference Not Set To An Instance Of An Object

This topic is 1738 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

Hi, In my Xna 3D Game. I made my own Terrain.cs Class in which I did the follows:

 

class Terrain
    {
        String LocationOfFile;

        GraphicsDevice Device;
        Viewport Viewport;

        Model Ground;

        Vector3 Position;

        Matrix World = Matrix.Identity;

        public Terrain()
        {

        }

        public void Initialize(GraphicsDevice D, Model Mesh, String LocationOfFile)
        {
            Ground = Mesh;
            Device = D;
            this.LocationOfFile = LocationOfFile;
            Viewport = Device.Viewport;
        }

        public void LoadContent(ContentManager Content, Vector3 Pos)
        {
            Ground = Content.Load<Model>(LocationOfFile);
            Position = Pos;
            World = Matrix.CreateTranslation(Position);
        }
    
        public void Draw(SpriteBatch spriteBatch, Camera.Camera Camera)
        {
            Matrix[] Transformations = new Matrix[Ground.Bones.Count];
            Ground.CopyAbsoluteBoneTransformsTo(Transformations);
            foreach (ModelMesh Mesh in Ground.Meshes)
            {
                foreach (BasicEffect effect in Mesh.Effects)
                {
                    effect.EnableDefaultLighting();
                    effect.View = Camera.View;
                    effect.Projection = Camera.Projection;
                    effect.World = Transformations[Ground.Bones.Count] * World;
                }
                Mesh.Draw();
            }
        }
    }

 

And from my Game1.cs Class I did this:

 

public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        Camera.Camera Camera;

        Terrain.Terrain Terrain;
        Model Land;

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

        protected override void Initialize()
        {
            graphics.PreferredBackBufferHeight = 600;
            graphics.PreferredBackBufferWidth = 800;
            Camera = new Camera.Camera(this.GraphicsDevice, Vector3.Zero, new Vector3(0.0f, 20.0f, 0.0f), 1.0f, 1000.0f);
            Terrain = new Life_As_We_Know_It.Terrain.Terrain();
            Terrain.Initialize(this.GraphicsDevice, Land, "Terrains\\Land");
            base.Initialize();
        }

        protected override void LoadContent()
        { 
            spriteBatch = new SpriteBatch(GraphicsDevice);
            
            // TODO: use this.Content to load your game content here
        }

        protected override void UnloadContent()
        {

        }

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

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

            Terrain.Draw(spriteBatch, Camera);
            base.Draw(gameTime);
        }
    }

 

But when I try to draw my Terrain Object derived from Terrain.cs Class I get the following error:

 

Object Reference Not Set To An Instance Of An Object In the Terrain.cs Class on Line "Matrix[] Transformations = new Matrix[Ground.Bones.Count];"

Share this post


Link to post
Share on other sites
Advertisement

Never Mind. Found The Problem. I forgot to call Terrain.LoadContent(Content, Vector3.Zero) in LoadContent Function of Game1.cs Class

Share this post


Link to post
Share on other sites

Just as future reference: This kind of situation is very easy to debug. When you get this exception, Visual Studio should break at the problematic line. This particular error message means an object is not instanced (null pointer), so you just check every object on the line by moving the mouse over and you will see that in this case either "Ground" or "Bones" will be null. Check when it was supposed to be loaded and here's your problem.

Share this post


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

  • Advertisement