   Trouble with classes
  Ok so I followed a tutorial and got a pretty nice terrain engine with nice water, moving clouds, and billboard trees. That however is where the tutorials stopped. I am now trying to expand on it with my growing knowledge of C# and XNA but I am having problems with classes. The tutorial had everything located with in the game1.cs file but it is getting kind of long and so I would like to put the terrain stuff into one class, camera into another class, audio into another class and so on. I decided that instead of trying to convert what I had I should try and start over and reorganize everything so here is what I have so farTerrain.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;using Microsoft.Xna.Framework.Net;using Microsoft.Xna.Framework.Storage;namespace Game_Engine{    class Terrain    {   VertexPositionColor[] vertices;   private VertexDeclaration myVertexDeclaration;   public GraphicsDevice device;   public GraphicsDevice Device   {  get { return device; }  set { device = value; }   }   public void SetUpVertices()   {  vertices = new VertexPositionColor[3];  vertices[0].Position = new Vector3(-0.5f, -0.5f, 0f);  vertices[0].Color = Color.Red;  vertices[1].Position = new Vector3(0, 0.5f, 0f);  vertices[1].Color = Color.Green;  vertices[2].Position = new Vector3(0.5f, -0.5f, 0f);  vertices[2].Color = Color.Yellow;  this.myVertexDeclaration = new VertexDeclaration(this.device, VertexPositionColor.VertexElements);   }   public void Draw(GraphicsDevice device)   {  device.VertexDeclaration = myVertexDeclaration;  device.DrawUserPrimitives(PrimitiveType.TriangleList, vertices, 0, 1);   }    }}Game1.cs:using System;using System.Collections.Generic;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.Net;using Microsoft.Xna.Framework.Storage;namespace Game_Engine{    public class Game1 : Microsoft.Xna.Framework.Game    {   GraphicsDeviceManager graphics;   SpriteBatch spriteBatch;   GraphicsDevice device;   Effect effect;   VertexDeclaration myVertexDeclaration;   Terrain terrain;   public Game1()   {  graphics = new GraphicsDeviceManager(this);  Content.RootDirectory = "Content";   }   protected override void Initialize()   {  graphics.PreferredBackBufferWidth = 500;  graphics.PreferredBackBufferHeight = 500;  graphics.IsFullScreen = false;  graphics.ApplyChanges();  Window.Title = "Riemer's XNA Tutorials -- Series 1";  terrain = new Terrain();    base.Initialize();   }   protected override void LoadContent()   {  device = graphics.GraphicsDevice;  spriteBatch = new SpriteBatch(GraphicsDevice);  effect = Content.Load<Effect>("effects");  terrain.SetUpVertices();}   protected override void UnloadContent()   {   }   protected override void Update(GameTime gameTime)   {  if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) this.Exit();  base.Update(gameTime);   }   protected override void Draw(GameTime gameTime)   {  device.Clear(Color.DarkSlateBlue);  effect.CurrentTechnique = effect.Techniques["Pretransformed"];  effect.Begin();  foreach (EffectPass pass in effect.CurrentTechnique.Passes)  { pass.Begin(); terrain.Draw(device); pass.End();  }  effect.End();  base.Draw(gameTime);   }    }}If any one could help me figure this out I would be very happy!The error I am getting is:Object reference not set to an instance of an object.and it is referring to the bold part in Terrain.cs   
  The device variable in your Terrain class is null.You need to pass through a reference to the device from your main Game1 class, preferably through the constructor of you Terrain class.Add the following to your Terrain class:public Terrain(GraphicsDevice device){   this.device = device}Then in your main Game1 class replace:terrain = new Terrain();With:terrain = new Terrain(graphics.GraphicsDevice);Also, the "this"s on this line are redundant:this.myVertexDeclaration = new VertexDeclaration(this.device, VertexPositionColor.VertexElements);;  Ok Now I have a new problem yet same type of problem. I am now trying to setup a class for the camera. If I set it up the same way I set up the terrain class then I get just the plain window with nothing drawn but if I don't put the device stuff then I get the same error I was getting with the terrain class:Object reference not set to an instance of an object.Here is the camera class. the way it is currently setup I get only the windowusing 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;using Microsoft.Xna.Framework.Net;using Microsoft.Xna.Framework.Storage;namespace Game_Engine{    class Camera    {   Matrix viewMatrix;   Matrix projectionMatrix;   private GraphicsDevice device;   public Camera(GraphicsDevice device)   {  this.device = device;   }   public void SetUpCamera()   {  viewMatrix = Matrix.CreateLookAt(new Vector3(0, 0, 50), new Vector3(0, 0, 0), new Vector3(0, 1, 0));  projectionMatrix = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, device.Viewport.AspectRatio, 1.0f, 300.0f);   }  }}Where I get the error When its the other way is with the projectionMatrix   ;  If you're just setting up a camera all you should see is the window. If you're not drawing anything you shouldn't expect to see anything.Anything you draw needs to be able to get at the camera's view and projection matrices to be able to draw them in view space.   ;  Ok I have stopped working on the camera class for right now and just included the camera in the game1.cs file. I kept on working had it so I could draw an array of Triangles then I changed my setupvertices method and I started once again getting a null reference exception on the setupvertices method.vertices[x + y * terrainWidth].Position = new Vector3(x, heightData[x, y], -y);that is where I am getting the null reference exception. I guess I am just not getting what a null reference exception means. It says to use new which I do use new there so I am not sure what is going on.Here is my terrain.cs and game1.cs code files:Terrain.csusing 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;using Microsoft.Xna.Framework.Net;using Microsoft.Xna.Framework.Storage;namespace Game_Engine{    class Terrain    {   VertexPositionColor[] vertices;   public VertexDeclaration myVertexDeclaration;   private GraphicsDevice device;   int[] indices;   private int terrainWidth = 4;   private int terrainHeight = 3;   private float[,] heightData;   public Terrain(GraphicsDevice device)   {  this.device = device;   }   public void SetUpVertices()   {  vertices = new VertexPositionColor[terrainWidth * terrainHeight];  for (int x = 0; x < terrainWidth; x++)  { for (int y = 0; y < terrainHeight; y++) {vertices[x + y * terrainWidth].Position = new Vector3(x, heightData[x, y], -y);vertices[x + y * terrainWidth].Color = Color.White; }  }  myVertexDeclaration = new VertexDeclaration(device, VertexPositionColor.VertexElements);   }   public void SetUpIndices()   {  indices = new int[(terrainWidth - 1) * (terrainHeight - 1) * 6];  int counter = 0;  for (int y = 0; y < terrainHeight - 1; y++)  { for (int x = 0; x < terrainWidth - 1; x++) {int lowerLeft = x + y * terrainWidth;int lowerRight = (x + 1) + y * terrainWidth;int topLeft = x + (y + 1) * terrainWidth;int topRight = (x + 1) + (y + 1) * terrainWidth;indices[counter++] = topLeft;indices[counter++] = lowerRight;indices[counter++] = lowerLeft;indices[counter++] = topLeft;indices[counter++] = topRight;indices[counter++] = lowerRight; }  }   }   public void LoadHeightData()   {  heightData = new float[4, 3];  heightData[0, 0] = 0;  heightData[1, 0] = 0;  heightData[2, 0] = 0;  heightData[3, 0] = 0;  heightData[0, 1] = 0.5f;  heightData[1, 1] = 0;  heightData[2, 1] = -1.0f;  heightData[3, 1] = 0.2f;  heightData[0, 2] = 1.0f;  heightData[1, 2] = 1.2f;  heightData[2, 2] = 0.8f;  heightData[3, 2] = 0;   }   public void Draw(GraphicsDevice device)   {  device.VertexDeclaration = myVertexDeclaration;  device.DrawUserIndexedPrimitives(PrimitiveType.TriangleList, vertices, 0, vertices.Length, indices, 0, indices.Length / 3);   }    }    class Skydome    {    }    class DNCycle    {    }    class Weather    {    }    class Water    {    }}Game1.csusing System;using System.Collections.Generic;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.Net;using Microsoft.Xna.Framework.Storage;namespace Game_Engine{    public class Game1 : Microsoft.Xna.Framework.Game    {   GraphicsDeviceManager graphics;   SpriteBatch spriteBatch;   Effect effect;   GraphicsDevice device;   Matrix viewMatrix;   Matrix projectionMatrix;   private float angle = 0f;   Terrain terrain;   devicemanager graphicsman;   public Game1()   {  graphics = new GraphicsDeviceManager(this);  Content.RootDirectory = "Content";   }   protected override void Initialize()   {  graphics.PreferredBackBufferWidth = 800;  graphics.PreferredBackBufferHeight = 600;  graphics.IsFullScreen = true;  graphics.ApplyChanges();  Window.Title = "Game Engine Alpha";  graphicsman = new devicemanager();  graphicsman.graphicsmanager();  terrain = new Terrain(graphics.GraphicsDevice);    base.Initialize();   }   protected override void LoadContent()   {  device = graphics.GraphicsDevice;  spriteBatch = new SpriteBatch(GraphicsDevice);  effect = Content.Load<Effect>("effects");  terrain.SetUpVertices();  terrain.SetUpIndices();  terrain.LoadHeightData();  SetUpCamera();}   public void SetUpCamera()   {  viewMatrix = Matrix.CreateLookAt(new Vector3(0, 10, 0), new Vector3(0, 0, 0), new Vector3(0, 0, -1));  projectionMatrix = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, device.Viewport.AspectRatio, 1.0f, 300.0f);   }   protected override void UnloadContent()   {   }   protected override void Update(GameTime gameTime)   {  if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) this.Exit();  angle += 0.005f;  base.Update(gameTime);   }   protected override void Draw(GameTime gameTime)   {  device.Clear(Color.DarkSlateBlue);  device.RenderState.CullMode = CullMode.None;  device.RenderState.FillMode = FillMode.WireFrame;  effect.CurrentTechnique = effect.Techniques["Colored"];  effect.Parameters["xView"].SetValue(viewMatrix);  effect.Parameters["xProjection"].SetValue(projectionMatrix);  effect.Parameters["xWorld"].SetValue(Matrix.Identity);  Vector3 rotAxis = new Vector3(3 * angle, angle, 2 * angle);  rotAxis.Normalize();  Matrix worldMatrix = Matrix.Identity;  effect.Parameters["xWorld"].SetValue(worldMatrix);  effect.Begin();  foreach (EffectPass pass in effect.CurrentTechnique.Passes)  { pass.Begin(); terrain.Draw(device); pass.End();  }  effect.End();  base.Draw(gameTime);   }    }};  You are trying to set up your vertices before calling your LoadHeightData function, meaning your heightData array is empty, ie. null.NullReferenceExceptions are usually pretty easy to figure out. Basically it means you've not initialised one of the variables you are reading from/passing to a method.How much C# did you know before you used Riemers tutorials? They are good for getting going with XNA, but assume you vaguely know what you're doing with C#.I'd recommend doing some learning based around C# before going much further. There are plenty of good sites out there with free tutorials.If you're too lazy for that then download a sample from the Creators club, or something not too complex from Ziggyware and try figuring out how the various clases etc. interact within it. Although I wouldn't recommend this method for you, as it seems you're not that experienced with OO languages or programing in general.PS. For big chunks of code use source tags in your post, rather than code ones.   ;  I Knew some C# I knew some of the very basics of C++, C#, Java, Python, and PHP (My knowledge of PHP has helped me a-lot! I can't believe how much PHP and C# are alike in code structure.)I also have all of the 3D Buzz XNA 101 Video tutorials (all over 200 hours worth) which teach you C# and XNA at the same time. I have watched over 60 of those video tutorials so far.   ;  Ok another question. I am not going to post all my code just the parts I am working on. I am trying to create a class for keyboard input. I create the classclass KeyboardControl    {   private float angle = 0f;   public void Update()   {  KeyboardState keyState = Keyboard.GetState();  if (keyState.IsKeyDown(Keys.Delete)) angle += 0.05f;  if (keyState.IsKeyDown(Keys.PageDown)) angle -= 0.05f;   }    }I then Initialize it like I shouldKeyboardControl control;control = new KeyboardControl();Then in the update method I put control.Update(); Everything builds just fine and it displays my lovely wire frame terrain mesh but when I push the page down and delete keys it doesn't spin around like it should. I did add the code to the draw method to take into account the rotation so its not that. When I place the KeyboardState code right into the update method it works fine but its not working from the keyboardcontrol class.   ;  Your keyboard class is only updating it's internal angle variable, which you've declared as private and so is not accessable from outside the class.Whereas I'm guessing either your Game1 class or Terrain class has an angle variable, which when you put the Keyboard code in there it's updating and this is the one it's using.(I did say go learn more about C# and OO programing...)Personally I'm not a fan of putting input management into a seperate class, I tend to keep the input code in with the relavent class the input is updating. I'm not saying this is the right way of doing it, I'm just offering it up for suggestion.   