I am greating an Array on UFO type (just a class i created)
When I try instantiate any. or all for that matter, of the elements in the array I get the following
error
An unhandled exception of type 'System.NullReferenceException' occurred in spaceInvaders.exe
Additional information: Object reference not set to an instance of an object.
and points to this line in th code
item.X = screen.Height / 2;
here is the code for that part
and the UFO class isCode:public class Game1 : Microsoft.Xna.Framework.Game { GraphicsDeviceManager graphics; SpriteBatch spriteBatch; Viewport screen; //create background image Texture2D background; Rectangle mainFrame; UFO[] aliensArr = new UFO[5]; protected override void Initialize() { screen = graphics.GraphicsDevice.Viewport; foreach (UFO item in aliensArr) { item.X = screen.Height / 2; item.Y = screen.Width / 2; item.Width = 75.0f; item.Height = 50.0f; } base.Initialize(); } ...... }
any ideas why i am getting this?Code:class UFO { public Texture2D Visual { get; set; } public float X { get; set; } public float Y { get; set; } public float Width { get; set; } public float Height { get; set; } public Rectangle Rect { get { return new Rectangle((int)X, (int)Y, (int)Width, (int)Height); } } }




Reply With Quote