For getting the correct level in the game I’m working on, I want to have and integer “level” and add one to it when the level is completed. Then in my “Levels” class, I have it set it to the appropriate level. This isn’t working though. Here’s some code to explain -
Player class
int[][] map = new int[6][8]; //initialize map, where level is stored
int level = 1;
//
//later on
//
public void checkCollision()
{
if (px == maxPosX) px = 8;
if (px == minPosX) px = 0;
if (py == maxPosY) py = 6;
if (py == minPosY) py = 0;
if (starting)
{
l.getMap(level);
for (int x = 0; x < 9; x++)
{
for (int y = 0; y < 7; y++)
{
System.out.println(x + " x, " + y + " y");
playerData = map[py][px];
mapData = map[y][x];
if (mapData == 0)
{
px = x;
py = y;
}
if (mapData == 2)
{
Boulder b = new Boulder(m, this);
b.setX(x);
b.setY(y);
m.boulders.add(b);
}
if (mapData == 3)
{
Pit pi = new Pit(m, this);
pi.setX(x);
pi.setY(y);
m.pits.add(pi);
}
if (mapData == 4)
{
Grass gr = new Grass(m, this);
gr.setX(x);
gr.setY(y);
m.grass.add(gr);
}
}
}
starting = false;
}
}
Levels class
package main;
public class Levels
{
Player p;
int[][] leveltest = {
{1, 1, 1, 1, 1, 1, 1, 1, 1,}, //
{1, 1, 1, 1, 1, 1, 1, 1, 1,}, //
{1, 1, 1, 1, 1, 1, 1, 1, 1,}, //
{1, 1, 1, 1, 1, 1, 1, 1, 1,}, //
{1, 1, 1, 1, 1, 1, 1, 1, 1,}, //
{1, 1, 1, 1, 1, 1, 1, 0, 1,}, //
{1, 1, 1, 1, 1, 1, 1, 1, 1,} //
};
int[][] level1 = {
{4, 4, 4, 4, 4, 4, 4, 4, 4,}, //
{4, 4, 4, 1, 3, 3, 4, 4, 4,}, //
{4, 4, 4, 2, 2, 2, 4, 4, 4,}, //
{4, 4, 1, 3, 0, 3, 4, 4, 4,}, //
{4, 4, 1, 2, 3, 2, 4, 4, 4,}, //
{4, 4, 4, 4, 1, 1, 4, 4, 4,}, //
{4, 4, 4, 4, 4, 4, 4, 4, 4,} //
};
public void getMap(int level)
{
if (level == 1) p.map = leveltest;
if (level == 2) p.map = level1;
}
}
I get a nullpointer exception on this line -
l.getMap(level);
Any help as to why this is happening would be appreciated.
Thanks,
-Nathan
EDIT - Source + Jar