We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › NullPointerException
Page Index Toggle Pages: 1
NullPointerException (Read 290 times)
NullPointerException
Feb 15th, 2010, 3:25pm
 
Everytime I try to run the program I keep getting the "NullPointerException" error message. Along with this other stuff:
Exception in thread "Animation Thread" java.lang.NullPointerException
     at Project_2_.setup(Project_2_.java:40)
     at processing.core.PApplet.handleDraw(PApplet.java:1402)
     at processing.core.PApplet.run(PApplet.java:1327)
     at java.lang.Thread.run(Thread.java:613)

Here's my code.

Code:
String[] rows = {
"grass",
"water",
"water",
"water",
"water",
"grass",
"road",
"road",
"road",
"road",
"grass"};

Lane[] allLanes;

int grassColor = color(18, 173, 5);
int waterColor = color(18, 23, 227);
int roadColor = color(17, 17, 18);

void setup () {
size (700,500);
smooth ();
allLanes = new Lane[rows.length];
for(int i = 0; i < allLanes.length; i++) allLanes[i].drawLane();
}

void draw () {
for(int i = 0; i < allLanes.length; i++) allLanes[i].drawLane();

}

class Lane {
color laneColor = color(0);
int rowHeight = 0;
int yPos = 0;

Lane(String type, int yRow){
if(type.equals("grass")) this.laneColor = grassColor;
if(type.equals("water")) this.laneColor = waterColor;
if(type.equals("road")) this.laneColor = roadColor;

this.rowHeight = height / rows.length;
this.yPos = this.rowHeight * yRow;
}

int drawLane() {
fill (this.laneColor);

rect (0,this.yPos, width, this.rowHeight);

return yPos;
}
}




Any help would be appreciated.
Re: NullPointerException
Reply #1 - Feb 15th, 2010, 3:52pm
 
Quote:
void setup () {
  size (700,500);
  smooth ();
  allLanes = new Lane[rows.length];
  for(int i = 0; i < allLanes.length; i++) allLanes[i] = new Lane(rows[i], i);
}



Just because you have an array of Lanes, that doesn't mean you have created any Lanes to fill that array. Also you don't need to draw them in setup().
Page Index Toggle Pages: 1