1
Answer

[C#] Getting a NullReferenceException on a Timer method.

Peter Clement

Peter Clement

10y
1.1k
1

Full code for ease of viewing: https://gist.github.com/anonymous/7c0e645976add17e49a5[1]

I'm creating a Pacman game and I have ran into a bit of a problem. Before we start, I should explain how the map is created. Within the Cell.cs class there is a case statement which creates lines/dots and them assigns to each case. Within a text file I have arranged these cases into a pattern that takes the form of a pacman map.

The problem I am having is with a timer statement (line 243 of Gameboard class / line 4 in code snippet) throwing a NullReferenceException.

if (pacman.pacmanFacing == 0)         {               //check for cells type o, d and p                            if (cells[yPos + 1, xPos].CellType == 'o' || cells[yPos + 1, xPos].CellType == 'd' || cells[yPos + 1, xPos].CellType == 'p') 

I originally had these if else statements in a "Gameboard_KeyDown" method (Gameboard is the main form). They worked fine while in that method.

But I have to move them to a Timer method in order to animate the Pacman character. 16 images as found in the MovingPacman.cs file separated into 4 images for each direction are for this purpose. They are called by these lines of code:

           pacman.currentMouthPosition += 1;                     if (pacman.currentMouthPosition > 3) pacman.currentMouthPosition = 0; 

I should also mention that when I set the timer to 10000 milliseconds it does not throw an error while its default 100 does. However, moving the pacman does still not work in this forced state.


Answers (1)