As the title suggests, I’m real new to Processing (read: Two weeks in), and I’m having a NullPointerException when trying to do make a maze game for a final project. The issue is occuring on line 216
if(potentialxpos > walls[i].x && potentialxpos < (walls[i].x + walls[i].w) && potentialypos > walls[i].y && potentialypos < walls[i].y + walls[i].h){
Here’s the full code, apologies about the absolute mess:
//Wednesday, June the 19th
//A basic maze-navigation game, where the player must navigate the avatar from a starting point to an ending point.
//List of cannibalised programs: https://forum.processing.org/two/discussion/1028/maze by billautomata ,
avatar player;
Walls[] walls;
boolean win = false;
//int gridRows = 18;
//int gridColumns = 21;
void setup() {
size(1000, 800);
frameRate(10);
noCursor();
player = new avatar(525,615);
//Creating the empty array list for the walls.
walls = new Walls[272];
//Writing out ALL of the dimensions of ALL of the walls. Fun.
walls[1]=(new Walls(60,30,30,30, color(0)));walls[39]=(new Walls(30,30,30,180, color(0)));walls[76]=(new Walls(30,180,90,180, color(0)));
walls[2]=(new Walls(90,120,120,120, color(0)));walls[40]=(new Walls(120,120,90,120, color(0)));walls[77]=(new Walls(90,120,90,240, color(0)));
walls[3]=(new Walls(90,240,30,240, color(0)));walls[41]=(new Walls(30,240,30,300, color(0)));walls[78]=(new Walls(30,300,30,240, color(0)));
walls[4]=(new Walls(30,240,90,240, color(0)));walls[42]=(new Walls(90,240,90,210, color(0)));walls[79]=(new Walls(90,210,180,210, color(0)));
walls[5]=(new Walls(180,210,120,210, color(0)));walls[43]=(new Walls(120,210,120,270, color(0)));walls[80]=(new Walls(120,270,150,270, color(0)));
walls[6]=(new Walls(150,270,150,240, color(0)));walls[44]=(new Walls(150,240,150,300, color(0)));walls[81]=(new Walls(150,300,180,300, color(0)));
walls[7]=(new Walls(180,300,180,330, color(0)));walls[45]=(new Walls(180,330,120,330, color(0)));walls[82]=(new Walls(120,330,120,360, color(0)));
walls[8]=(new Walls(120,360,120,300, color(0)));walls[46]=(new Walls(120,300,90,300, color(0)));walls[83]=(new Walls(90,300,60,300, color(0)));
walls[9]=(new Walls(90,300,90,270, color(0)));walls[47]=(new Walls(90,270,60,270, color(0)));walls[84]=(new Walls(90,270,90,300, color(0)));
walls[10]=(new Walls(90,300,120,300, color(0)));walls[48]=(new Walls(120,300,120,330, color(0)));walls[85]=(new Walls(120,330,180,330, color(0)));
walls[11]=(new Walls(180,330,180,360, color(0)));walls[49]=(new Walls(180,360,240,360, color(0)));walls[86]=(new Walls(240,360,270,360, color(0)));
walls[12]=(new Walls(240,360,240,300, color(0)));walls[50]=(new Walls(240,300,270,300, color(0)));walls[87]=(new Walls(270,300,270,330, color(0)));
walls[13]=(new Walls(270,300,240,300, color(0)));walls[51]=(new Walls(240,300,240,360, color(0)));walls[88]=(new Walls(240,360,210,360, color(0)));
walls[14]=(new Walls(210,360,210,420, color(0)));walls[52]=(new Walls(210,420,180,420, color(0)));walls[89]=(new Walls(180,420,180,480, color(0)));
walls[15]=(new Walls(180,480,240,480, color(0)));walls[53]=(new Walls(240,480,240,510, color(0)));walls[90]=(new Walls(240,510,300,510, color(0)));
walls[16]=(new Walls(240,510,240,480, color(0)));walls[54]=(new Walls(240,480,150,480, color(0)));walls[91]=(new Walls(150,480,150,420, color(0)));
walls[17]=(new Walls(150,480,180,480, color(0)));walls[55]=(new Walls(180,480,180,510, color(0)));walls[92]=(new Walls(180,510,210,510, color(0)));
walls[18]=(new Walls(210,510,210,540, color(0)));walls[56]=(new Walls(210,510,180,510, color(0)));walls[93]=(new Walls(180,510,180,420, color(0)));
walls[19]=(new Walls(180,420,210,420, color(0)));walls[57]=(new Walls(210,420,210,390, color(0)));walls[94]=(new Walls(210,390,270,390, color(0)));
walls[20]=(new Walls(270,390,270,420, color(0)));walls[58]=(new Walls(270,420,300,420, color(0)));walls[95]=(new Walls(300,420,300,450, color(0)));
walls[21]=(new Walls(300,450,360,450, color(0)));walls[59]=(new Walls(360,450,360,420, color(0)));walls[96]=(new Walls(360,420,360,540, color(0)));
walls[22]=(new Walls(360,540,420,540, color(0)));walls[60]=(new Walls(420,540,420,480, color(0)));walls[97]=(new Walls(420,480,450,480, color(0)));
walls[23]=(new Walls(420,480,420,420, color(0)));walls[61]=(new Walls(420,420,390,420, color(0)));walls[98]=(new Walls(390,420,390,510, color(0)));
walls[24]=(new Walls(390,510,390,330, color(0)));walls[62]=(new Walls(390,330,390,360, color(0)));walls[99]=(new Walls(390,360,360,360, color(0)));
walls[25]=(new Walls(360,360,360,300, color(0)));walls[63]=(new Walls(360,300,420,300, color(0)));walls[100]=(new Walls(420,300,420,210, color(0)));
walls[26]=(new Walls(420,300,360,300, color(0)));walls[64]=(new Walls(360,300,360,210, color(0)));walls[101]=(new Walls(360,210,360,240, color(0)));
walls[27]=(new Walls(360,240,390,240, color(0)));walls[65]=(new Walls(390,240,390,270, color(0)));walls[102]=(new Walls(390,270,390,240, color(0)));
walls[28]=(new Walls(390,240,360,240, color(0)));walls[66]=(new Walls(360,240,360,360, color(0)));walls[103]=(new Walls(360,360,300,360, color(0)));
walls[29]=(new Walls(300,360,300,390, color(0)));walls[67]=(new Walls(300,390,360,390, color(0)));walls[104]=(new Walls(360,390,330,390, color(0)));
walls[30]=(new Walls(330,390,330,420, color(0)));walls[68]=(new Walls(330,390,300,390, color(0)));walls[105]=(new Walls(300,390,300,270, color(0)));
walls[31]=(new Walls(300,270,180,270, color(0)));walls[69]=(new Walls(180,270,210,270, color(0)));walls[106]=(new Walls(210,270,210,330, color(0)));
walls[32]=(new Walls(240,270,240,180, color(0)));walls[70]=(new Walls(240,180,270,180, color(0)));walls[107]=(new Walls(270,180,240,180, color(0)));
walls[33]=(new Walls(240,180,240,120, color(0)));walls[71]=(new Walls(240,120,300,120, color(0)));walls[108]=(new Walls(300,120,300,90, color(0)));
walls[34]=(new Walls(300,90,210,90, color(0)));walls[72]=(new Walls(210,90,210,150, color(0)));walls[109]=(new Walls(210,150,120,150, color(0)));
walls[35]=(new Walls(120,150,120,180, color(0)));walls[73]=(new Walls(120,180,210,180, color(0)));walls[110]=(new Walls(210,180,210,240, color(0)));
walls[36]=(new Walls(210,240,180,240, color(0)));walls[74]=(new Walls(120,150,150,150, color(0)));walls[111]=(new Walls(150,150,150,90, color(0)));
walls[37]=(new Walls(150,90,180,90, color(0)));walls[75]=(new Walls(180,90,180,120, color(0)));walls[112]=(new Walls(180,90,120,90, color(0)));
walls[38]=(new Walls(120,90,120,60, color(0)));
//Break due to starting a new 'mega-object'
walls[113]=(new Walls(390,360,480,360, color(0)));walls[132]=(new Walls(480,360,480,420, color(0)));walls[150]=(new Walls(480,420,510,420, color(0)));
walls[114]=(new Walls(510,420,510,450, color(0)));walls[133]=(new Walls(570,420,480,420, color(0)));walls[151]=(new Walls(480,420,480,360, color(0)));
walls[115]=(new Walls(480,360,540,360, color(0)));walls[134]=(new Walls(540,360,510,360, color(0)));walls[152]=(new Walls(510,360,510,300, color(0)));
walls[116]=(new Walls(510,300,600,300, color(0)));walls[135]=(new Walls(600,300,600,330, color(0)));walls[153]=(new Walls(600,330,600,240, color(0)));
walls[117]=(new Walls(600,240,570,240, color(0)));walls[136]=(new Walls(570,240,510,240, color(0)));walls[154]=(new Walls(570,240,570,210, color(0)));
walls[118]=(new Walls(510,240,510,270, color(0)));walls[137]=(new Walls(510,270,570,270, color(0)));walls[155]=(new Walls(570,270,480,270, color(0)));
walls[119]=(new Walls(480,270,480,300, color(0)));walls[138]=(new Walls(510,330,420,330, color(0)));walls[156]=(new Walls(420,330,450,330, color(0)));
walls[120]=(new Walls(450,330,450,240, color(0)));walls[139]=(new Walls(450,240,480,240, color(0)));walls[157]=(new Walls(480,240,480,210, color(0)));
walls[121]=(new Walls(480,210,540,210, color(0)));walls[140]=(new Walls(540,210,540,180, color(0)));walls[158]=(new Walls(540,180,600,180, color(0)));
walls[122]=(new Walls(600,180,600,210, color(0)));walls[141]=(new Walls(600,210,600,150, color(0)));walls[159]=(new Walls(600,150,600,180, color(0)));
walls[123]=(new Walls(600,180,570,180, color(0)));walls[142]=(new Walls(570,180,570,30, color(0)));walls[160]=(new Walls(570,30,570,60, color(0)));
walls[124]=(new Walls(570,60,540,60, color(0)));walls[143]=(new Walls(540,60,570,60, color(0)));walls[161]=(new Walls(570,60,570,120, color(0)));
walls[125]=(new Walls(570,120,540,120, color(0)));walls[144]=(new Walls(540,120,540,150, color(0)));walls[162]=(new Walls(570,120,570,180, color(0)));
walls[126]=(new Walls(570,180,510,180, color(0)));walls[145]=(new Walls(510,180,510,30, color(0)));walls[163]=(new Walls(510,30,540,30, color(0)));
walls[127]=(new Walls(510,90,540,90, color(0)));walls[146]=(new Walls(510,90,510,120, color(0)));walls[164]=(new Walls(510,120,480,120, color(0)));
walls[128]=(new Walls(510,120,510,180, color(0)));walls[147]=(new Walls(510,180,450,180, color(0)));walls[165]=(new Walls(450,180,450,210, color(0)));
walls[129]=(new Walls(450,180,420,180, color(0)));walls[148]=(new Walls(420,180,420,150, color(0)));walls[166]=(new Walls(420,150,390,150, color(0)));
walls[130]=(new Walls(390,150,390,120, color(0)));walls[149]=(new Walls(390,120,360,120, color(0)));walls[167]=(new Walls(390,120,390,90, color(0)));
walls[131]=(new Walls(390,90,420,90, color(0)));
//As the previous note, new 'mega-object'
walls[168]=(new Walls(90,0,90,90, color(0)));walls[170]=(new Walls(90,90,60,90, color(0)));walls[171]=(new Walls(60,90,60,60, color(0)));
walls[169]=(new Walls(60,60,60,120, color(0)));
//Ibid
walls[172]=(new Walls(0,0,630,0, color(0)));walls[174]=(new Walls(630,0,630,540, color(0)));walls[175]=(new Walls(630,540,630,0, color(0)));
walls[173]=(new Walls(630,540,0,540, color(0)));
//Ibid
walls[176]=(new Walls(30,330,90,330, color(0)));walls[184]=(new Walls(90,330,90,360, color(0)));walls[192]=(new Walls(90,330,60,330, color(0)));
walls[177]=(new Walls(60,330,60,390, color(0)));walls[185]=(new Walls(60,390,180,390, color(0)));walls[193]=(new Walls(180,390,150,390, color(0)));
walls[178]=(new Walls(150,390,150,360, color(0)));walls[186]=(new Walls(150,360,150,390, color(0)));walls[194]=(new Walls(150,390,120,390, color(0)));
walls[179]=(new Walls(120,390,120,450, color(0)));walls[187]=(new Walls(120,450,90,450, color(0)));walls[195]=(new Walls(90,450,90,480, color(0)));
walls[180]=(new Walls(120,450,120,510, color(0)));walls[188]=(new Walls(120,510,60,510, color(0)));walls[196]=(new Walls(60,510,60,480, color(0)));
walls[181]=(new Walls(60,480,30,480, color(0)));walls[189]=(new Walls(60,420,90,420, color(0)));walls[197]=(new Walls(90,420,30,420, color(0)));
walls[182]=(new Walls(30,420,30,450, color(0)));walls[190]=(new Walls(30,420,60,420, color(0)));walls[198]=(new Walls(60,420,60,510, color(0)));
walls[183]=(new Walls(60,510,150,510, color(0)));walls[191]=(new Walls(150,510,150,540, color(0)));
//Ibid
walls[199]=(new Walls(0,510,30,510, color(0)));
//Ibid
walls[200]=(new Walls(0,360,30,360, color(0)));walls[201]=(new Walls(30,360,30,390, color(0)));
//Ibid
walls[202]=(new Walls(180,0,180,30, color(0)));walls[203]=(new Walls(180,30,240,30, color(0)));
//Ibid
walls[204]=(new Walls(270,0,270,60, color(0)));walls[213]=(new Walls(270,60,150,60, color(0)));walls[222]=(new Walls(150,60,150,30, color(0)));
walls[205]=(new Walls(150,30,120,30, color(0)));walls[214]=(new Walls(150,30,150,60, color(0)));walls[223]=(new Walls(150,60,330,60, color(0)));
walls[206]=(new Walls(330,60,330,30, color(0)));walls[215]=(new Walls(330,30,300,30, color(0)));walls[224]=(new Walls(300,30,360,30, color(0)));
walls[207]=(new Walls(360,30,330,30, color(0)));walls[216]=(new Walls(330,30,330,150, color(0)));walls[225]=(new Walls(330,150,360,150, color(0)));
walls[208]=(new Walls(360,150,360,180, color(0)));walls[217]=(new Walls(360,180,390,180, color(0)));walls[226]=(new Walls(390,180,390,210, color(0)));
walls[209]=(new Walls(390,210,390,180, color(0)));walls[218]=(new Walls(390,180,330,180, color(0)));walls[227]=(new Walls(330,180,330,330, color(0)));
walls[210]=(new Walls(330,330,330,240, color(0)));walls[219]=(new Walls(330,240,300,240, color(0)));walls[228]=(new Walls(300,240,330,240, color(0)));
walls[211]=(new Walls(330,240,330,180, color(0)));walls[220]=(new Walls(330,180,300,180, color(0)));walls[229]=(new Walls(300,180,300,150, color(0)));
walls[212]=(new Walls(300,150,270,150, color(0)));walls[221]=(new Walls(300,210,270,210, color(0)));walls[230]=(new Walls(270,210,270,240, color(0)));
//Ibid
walls[231]=(new Walls(390,0,390,30, color(0)));
//Ibid
walls[232]=(new Walls(480,0,480,90, color(0)));walls[236]=(new Walls(480,90,480,60, color(0)));walls[240]=(new Walls(480,60,360,60, color(0)));
walls[233]=(new Walls(360,60,360,90, color(0)));walls[237]=(new Walls(420,60,420,30, color(0)));walls[241]=(new Walls(420,30,450,30, color(0)));
walls[234]=(new Walls(420,30,420,60, color(0)));walls[238]=(new Walls(420,60,450,60, color(0)));walls[242]=(new Walls(450,60,450,150, color(0)));
walls[235]=(new Walls(450,150,480,150, color(0)));walls[239]=(new Walls(450,120,420,120, color(0)));
//Ibid
walls[243]=(new Walls(600,0,600,120, color(0)));
//Ibid
walls[244]=(new Walls(510,390,600,390, color(0)));walls[247]=(new Walls(600,390,600,450, color(0)));walls[250]=(new Walls(600,450,600,540, color(0)));
walls[245]=(new Walls(600,450,540,450, color(0)));walls[248]=(new Walls(540,450,540,510, color(0)));walls[251]=(new Walls(540,510,510,510, color(0)));
walls[246]=(new Walls(510,510,570,510, color(0)));walls[249]=(new Walls(570,510,570,480, color(0)));
//Ibid
walls[252]=(new Walls(480,540,480,510, color(0)));walls[255]=(new Walls(480,510,450,510, color(0)));walls[258]=(new Walls(450,510,480,510, color(0)));
walls[253]=(new Walls(480,510,480,480, color(0)));walls[256]=(new Walls(480,480,510,480, color(0)));walls[259]=(new Walls(480,480,480,450, color(0)));
walls[254]=(new Walls(480,450,450,450, color(0)));walls[257]=(new Walls(450,450,450,390, color(0)));walls[260]=(new Walls(450,390,420,390, color(0)));
//Ibid
walls[261]=(new Walls(630,360,570,360, color(0)));walls[262]=(new Walls(570,360,570,330, color(0)));walls[263]=(new Walls(570,330,540,330, color(0)));
//Ibid
walls[264]=(new Walls(330,540,330,480, color(0)));walls[267]=(new Walls(330,480,270,480, color(0)));walls[269]=(new Walls(270,480,270,450, color(0)));
walls[265]=(new Walls(270,450,240,450, color(0)));walls[268]=(new Walls(240,450,240,420, color(0)));walls[270]=(new Walls(240,420,240,450, color(0)));
walls[266]=(new Walls(240,450,210,450, color(0)));
//Ibid
walls[271]=(new Walls(0,210,60,210, color(0)));
}
void draw() {
background(#FFFFFF);
//Drawing the avatar
player.display();
player.move(walls);
//Actually drawing out the walls
for(int i = 0; i < walls.length; i++) {
walls[i].display();
}
//Making the red box for the target.
fill(#FF1A00);
quad(0,510,30,510,30,540,0,540);
}
class Walls {//Creating a class for the Walls that are drawn up in setup
int x;
int y;
int w;
int h;
color wallColor;
Walls(
int tempX, int tempY, int tempH,
int tempW, color tempWallColor) {
x = tempX;
y = tempY;
w = tempW;
h = tempH;
wallColor = tempWallColor;
}
void display() {
stroke(wallColor);
fill(wallColor);
rect (x,y,30,0);
}
}
class avatar {//Class for the avatar that will be moved around on the screen, with stuff like the win condition coded into the class.
float xpos;
float ypos;
avatar(
float _xpos, float _ypos) {
xpos = _xpos;
ypos = _ypos;
}
void display () {
stroke(255);
fill(255);
ellipse(xpos,ypos,15,15);
}
void move(Walls[] walls){
float potentialxpos = xpos;
float potentialypos = ypos;
if (win == false) {
if (keyPressed==true) {
println(key);
if(key == 'a' || key == 'A') {
potentialxpos = potentialxpos - 3;
}
if(key == 'd' || key == 'D') {
potentialxpos = potentialxpos +3;
}
if(key == 'w' || key == 'W') {
potentialypos = potentialypos - 3;
}
if(key == 's' || key == 'S') {
potentialypos = potentialypos +3;
}
}
}
boolean collide = false;
for(int i = 0; i < walls.length; i++){
if(potentialxpos > walls[i].x && potentialxpos < (walls[i].x + walls[i].w) && potentialypos > walls[i].y && potentialypos < walls[i].y + walls[i].h){
collide = true;
}
}
if(collide == false){
xpos = potentialxpos;
ypos = potentialypos;
}
}
void winCondition() {
if (xpos == 15 && ypos == 525) {
fill(#00FF86);
textAlign(CENTER,CENTER);
text("You win!",width/2,height/2);
}
}
}