Jump to content

  • Log In with Google      Sign In   
  • Create Account


Object Reference Not Set To An Instance Of An Object


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 CoOlDud3   Members   -  Reputation: 151

Like
0Likes
Like

Posted 15 April 2013 - 08:57 AM

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];"



Sponsor:

#2 CoOlDud3   Members   -  Reputation: 151

Like
0Likes
Like

Posted 15 April 2013 - 09:49 AM

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



#3 Dunge   Members   -  Reputation: 405

Like
1Likes
Like

Posted 15 April 2013 - 10:57 AM

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.



#4 CoOlDud3   Members   -  Reputation: 151

Like
0Likes
Like

Posted 21 April 2013 - 06:19 AM

Thank you =)






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS