Null pointer exception


(Deep Fried Ian) #1

Hi im new to the forums so please forgive anything I am not doing right.

Anyways, iv been trying to write a game lately and iv hit a small bump. My game is a platformer and im trying to switch between different maps. My game runs just fine but when i switch to the other map it gives me a null pointer exception and dies. The problem is i don’t know what is causing this, any help would be appreciated :3

btw to switch maps just keep walking to the left, arrow keys to move/jump.

I know im probably doing some things wrong but I am quite new to this. Any suggestions or constructive criticism would also be nice. thanks for your time :smiley:


(Ultima2876) #2

Hello, welcome to the community :smile:

A quick look shows that the problem is here:

currentMap = maps[currentMapIndex];
			
level = new Level(currentMap.xml);

currentMap is being set to null because the currentMapIndex is 2, which is out of the range of the maps[] array (it contains 2 entries at this point, which would be accessed using a map index of 0 or 1). When it tries to do the currentMap.xml part, it can’t because currentMap is null. You probably want to check for this condition and handle it appropriately;

if(currentMap != null)
{
 level = new Level(currentMap.xml);
}
else //do something

However, the more important thing is why currentMapIndex is being set to 2.

It seems as if this function is being called every frame:

public function switchMap():Boolean {
			//check where the players have to go
			var direction:int = checkMapBoundaries();
			if (direction != NONE) {
				switch(currentMap.Lname) {
					case("level 1"):
						if (direction == LEFT) {
							currentMapIndex = 1;
							return true;
						}
						break;
						
					case("level 2"):
						if (direction == RIGHT) {
							currentMapIndex = 2;
							return true;
						}
						break;
				}
				
			}
			return false;
		}

and direction always seems to be coming out as 2, even when I am well within the bounds of the level. In FlashDevelop, if you click in the column to the left of your line numbers (it looks like a little white column gap thingy) it’ll put a little red dot. This is called a breakpoint; when you run the game, your debugging will pause if the code ‘hits’ that breakpoint at any time. So if you put a breakpoint on the “switch(currentMap.Lname) {” line, you can see if your code reaches it and investigate the state of all the variables at the time it does so. It seems to be getting to that point every single frame, with a direction of 2, which leads me to believe that your checkMapBoundaries function has a bug. The ultimate result of this is that the currentMapIndex gets set to 2 every single frame.

It’s time for bed though so that’s all I can do for now!


(Deep Fried Ian) #3

wow that was actually a really stupid mistake on my part XD i used to program a bit in java so learning this isnt too hard. Arrays just confuse me a lot :stuck_out_tongue: anyways thanks a lot, never would have noticed that myself :slight_smile: