0

I am working on a Space Invaders game and I got the invader anmation set up and working. Now I only nee to create the array holding the invaders and Update/Draw it. So I got an exception on the following code:

--Game1.cs--

Variables:

botInvaders[,] InvadersArray = new botInvaders[botInvaders.BotInvaderRows, botInvaders.BotInvaderCollumns];

Update():

for (int y = 0; y < botInvaders.BotInvaderRows; ++y)
{
    for (int x = 0; x < botInvaders.BotInvaderCollumns; ++x)
    {
        InvadersArray[x, y].Update(gameTime);
    }
}

Draw():

                for (int y = 0; y < botInvaders.BotInvaderRows; ++y)
                {
                    for (int x = 0; x < botInvaders.BotInvaderCollumns; ++x)
                    {
                        InvadersArray[x, y] = new botInvaders(botInvaders.BotInvaderTex);
                        InvadersArray[x, y].Draw(spriteBatch);
                    }
                }

--botInvaders.cs--

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Storage;
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;

namespace SpaceInvaders
{
    class botInvaders
    {
        Texture2D Texture;
        public botInvaders(Texture2D texture)
        {
            Texture = texture;   
        }

        public static Texture2D BotInvaderTex;
        Rectangle BotInvaderHitBox;
        public static Vector2 BotInvaderPos = new Vector2(0, 28), BotInvaderOrigin;

        public static int BotInvaderCurrentFrame = 1, BotInvaderFrameWidth = 52, BotInvaderFrameHeight = 88, BotInvaderRows = 10, BotInvaderCollumns = 5;

        float Timer = 0f, Interval = 100f;

        public void Initialize()
        {

        }

        public void LoadContent(ContentManager Content)
        {
            BotInvaderTex = Content.Load<Texture2D>(".\\gameGraphics\\gameSprites\\botInvaders\\normalInvaders\\invaderShip1");
        }

        public void Update(GameTime gameTime)
        {
            BotInvaderHitBox = new Rectangle(BotInvaderCurrentFrame * BotInvaderFrameWidth, 0, BotInvaderFrameWidth, BotInvaderFrameHeight);
            BotInvaderOrigin = new Vector2(BotInvaderHitBox.X / 2, BotInvaderHitBox.Y / 2);

            Timer += (float)gameTime.ElapsedGameTime.Milliseconds;

            if (Timer > Interval)
            {
                BotInvaderCurrentFrame++;
                Timer = 0f;
            }

            if (BotInvaderCurrentFrame == 2)
            {
                BotInvaderCurrentFrame = 0;
            }

            BotInvaderHitBox = new Rectangle(BotInvaderCurrentFrame * BotInvaderFrameWidth, 0, BotInvaderFrameWidth, BotInvaderFrameHeight);
            BotInvaderOrigin = new Vector2(BotInvaderHitBox.Width / 2, BotInvaderHitBox.Height / 2);
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(BotInvaderTex, BotInvaderPos, BotInvaderHitBox, Color.White, 0f, Vector2.Zero, 1.0f, SpriteEffects.None, 0);      
        }
    }
}

So what could be the source of the exception?

PowerUser
  • 812
  • 1
  • 11
  • 32
  • You have just defined an array, `InvadersArray`, you haven't initialized its each item. So when you do `InvadersArray[x, y].Update(gameTime);` you get null reference exception. – Habib Nov 21 '14 at 16:15
  • So how should I do that? `foreach` loop? – PowerUser Nov 21 '14 at 16:16
  • 1
    It has nothing to do with a `foreach` loop. All elements in your array points to `null`, Populate your array or do something like `InvadersArray[x, y] = new botInvaders();` – Habib Nov 21 '14 at 16:18
  • If I add that code I get index out of bounds exception and what do you mean by populate? [sorry, I'm noob at arrays :(] – PowerUser Nov 21 '14 at 16:30
  • In your `Update()` **before** calling `InvadersArray[x, y].Update(gameTime);` have a line `InvadersArray[x, y] = new botInvaders();` – Habib Nov 21 '14 at 16:32
  • But the exception is at `Draw()`... I edited the code in my question. – PowerUser Nov 21 '14 at 16:35
  • Try http://www.microsoftvirtualacademy.com/training-courses/c-fundamentals-for-absolute-beginners - I think you possibly need to start getting much more familiar with the language and constructs before diving in and writing a game. I dont want to squash your enthusiasm, but I do think you need to go back to basics a little bit before you start on somehting more advanced, especially you need to learn how to use a debugger to inspect variables in your code. – PhillipH Nov 21 '14 at 16:59
  • True I'm not that good developer and that's why I started with such a simple game. Now can you help me finish at least this chapter of my game and I'll take that training course? – PowerUser Nov 21 '14 at 17:04

0 Answers0