0

I keep getting 'System.NullReferenceException' occurred in PacmanGame.exe error at this part of the code:

if (cells[yPosition + 1, xPosition].CellType == 'o' 
                    || cells[yPosition + 1, xPosition].CellType == '.'
                    || cells[yPosition + 1, xPosition].CellType == '!') 

    public partial class Form1 : Form
    {
        const int cellSize = 20; //cell dimension in pixels
        // The two-dimensional array of Cells that will make up the maze 
        private Cell[,] cells;
        // the width of the map in tiles (NOT pixels)
        private int mapWidth;
        // the height of the map in tiles (NOT pixels)
        private int mapHeight;



        private Image[] pacmanImage = new Image[4];
        public int currentMouthPosition { get; set; }
        private Image[] ghostImage = new Image[2];
        private Random rnd = new Random();
        private int currentEyePosition { get; set; } //= 0;
        private int score = 0;
        private int lives = 3;

       // private PacmanMove pacman;


        private int xPositiong = 520;//  initial xPosition for the ghost

        private int yPositiong = 200;//  initial yPosition for the ghost

        private int xpos = 420;//  initial xPosition for the Pacman

        private int ypos = 260;//  initial yPosition for the Pacman


        public int pacmanisfaceing = 0;



 // timer1 is for pacman

        public void timer1_Tick(object sender, EventArgs e)
        {

            int xPosition = xpos / 20;
            int yPosition = ypos / 20;
           currentMouthPosition += 1;
            if (currentMouthPosition > 3) currentMouthPosition = 0;


            if (xPosition < 0) xPosition = this.Width;
            else if (xPosition > this.Width) xPosition = 0;

            if (yPosition < 0) yPosition = this.Height;
            else if (yPosition > this.Height) yPosition = 0;


            // if statement for detecting the walls so pacman wont go through them
            if (pacmanisfaceing == 0) // used to detect walls when pacman is facing down
            {
                if (cells[yPosition + 1, xPosition].CellType == 'o' 
                    || cells[yPosition + 1, xPosition].CellType == '.'
                    || cells[yPosition + 1, xPosition].CellType == '!')
                {

                  yPosition += 10;
                   // CheckCollision(); // checks if pacman has had a collision with a ghost

                    if (cells[yPosition + 1, xPosition].CellType == '.'
                        && (cells[yPosition, xPosition].IsVisible == true)) //if statement used detect a pill and if one is visible
                    {                                                                                                    // it will add up the number of pills lifted

                        cells[yPosition, xPosition].IsVisible = false; // this is the code for adding up the score of the pills
                        score += 10; // adds on 10 everytime a pill is lifted
                        ScoreInfoEventArgs currentScore = new ScoreInfoEventArgs(score);
                        OnScoreChanged(currentScore);
                    }

                    // if statement for detecting the power pills and adding on extra points
                    if (cells[yPosition + 1, xPosition].CellType == '!' && (cells[yPosition, xPosition].IsVisible == true))
                    {
                        cells[yPosition, xPosition].IsVisible = false;
                        score += 50; // adds on 50 points when a power pill is lifted
                        ScoreInfoEventArgs currentScore = new ScoreInfoEventArgs(score);
                        OnScoreChanged(currentScore);
                        //SoundPlayer myPlayerMove = new SoundPlayer("..\\..\\Sounds\\pacman_eatfruit.wav"); // plays the eat fruit tune when a power pill is lifted
                       // myPlayerMove.Play();
                    }

                }

            }
davidallyoung
  • 1,302
  • 11
  • 15
jim
  • 39
  • 3
  • You have to assign the array before using it and also arrays require a size. Probably change your `Cell[,]` line to read: `private Cell[,] cells = new Cell[cellSize, cellSize];` – Quantic Apr 06 '16 at 21:04
  • 3
    please read http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it?s=1|9.3699 – hatchet - done with SOverflow Apr 06 '16 at 21:07

1 Answers1

0

There is only two things that could possibly be null in that specific line

(cells[yPosition + 1, xPosition].CellType == 'o' || cells[yPosition + 1, xPosition].CellType == '.' || cells[yPosition + 1, xPosition].CellType == '!')
  • cells
  • the array element at cells[yPosition + 1, xPosition]

If cells is null, that's an easy fix -- just means that the object referenced by cells hasn't been instantiated. If the array element is null, your logic for the cell indices or for populating the cells array is wrong.

Joshua Evensen
  • 1,544
  • 1
  • 15
  • 33