-1

i'm doing a piece of C# coursework where I have had to make a Space Invaders style game. I am unsure as to how, but I keep running into this error

System.NullReferenceException

Object reference not set to an instance of an object

This is the code I currently have:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.Serialization;

namespace Space_Invaders_Clone
{
    public partial class Form1 : Form
    {

        int BulletsCreated = 0;
        PictureBox[] bullets = new PictureBox[999];

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            //MessageBox.Show(e.KeyValue.ToString());
            if (e.KeyValue == 37 && Turret.Left > 12)
            {
                Turret.Left = Turret.Left - 5;
            }

            if (e.KeyValue == 39 && Turret.Left < 593)
            {
                Turret.Left = Turret.Left + 5;
            }

            if (e.KeyValue == 32)
            {
                bullets[BulletsCreated] = new PictureBox();
                bullets[BulletsCreated].Width = 2;
                bullets[BulletsCreated].Height = 24;
                bullets[BulletsCreated].BackColor = Color.Cyan;
                bullets[BulletsCreated].Top = Turret.Top - 26;
                bullets[BulletsCreated].Left = Turret.Left + Turret.Width / 2;
                bullets[BulletsCreated].Enabled = true;
                bullets[BulletsCreated].Visible = true;
                this.Controls.Add(bullets[BulletsCreated]); //add to controls
                BulletsCreated++;
            }
        }



                    private void BulletTimer_Tick(object sender, EventArgs e)
        {

            bullets[0].Top = bullets[0].Top - 10;
        }



    }


}

The Error Occurs on this line :

bullets[0].Top = bullets[0].Top - 10;

What have I done wrong?

1 Answers1

2

The method BulletTimer_Tick is being invoked before the keyvalue 32 is handled. So you attempt to read and update a value at address 0 which doesn't exist.

LordWilmore
  • 2,829
  • 2
  • 25
  • 30
  • "PictureBox[] bullets = new PictureBox[999];" I thought this code would have been executed first, creating the value I need? I tried changing it from 0 to 1 anyway and still the same error. Do I have to move my code around or merely define what bullets[0] is before the rest of the code is done? – Charles King Jan 29 '18 at 14:19
  • 1
    No, all you've done is create an array that will contain 999 default implementations of PictureBox. The default value for a PictureBox (given it's a reference type) is null, so you've got an array of null values. You need to actually instantiate them if you want actual instances. Drop a breakpoint on and take a look. – LordWilmore Jan 29 '18 at 14:20
  • Okay, How do I add a value to the picture boxes? Also where do I put the breakpoint? – Charles King Jan 29 '18 at 14:23
  • @CharlesKing Put the breakpoint where you are getting the exception, or after you create the array - it's up to you really. All I'm saying is that you will see the array is full of null values. As for how to create instances of PictureBox, well you're probably best off reading the documentation for that. – LordWilmore Jan 29 '18 at 14:34