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 & HelpPrograms › Strange NullPointerException Error, Please Help!
Page Index Toggle Pages: 1
Strange NullPointerException Error, Please Help! (Read 599 times)
Strange NullPointerException Error, Please Help!
Apr 21st, 2009, 4:10am
 
This is my first program in Processing, BTW.
Code:
//Fish by Jonah C.

//Initialize fish and rocks
Fish fish1;
Fish fish2;
Fish fish3;
Fish biggie;
Rock rock1;
Rock rock2;
Rock small1;
Rock small2;
Rock small3;
Rock big1;
Chomp[] bites=new Chomp[1];
int chompCount;

 
void setup() {
//set size and background
 size(1000,400);
 background(255);
//set chomp-array counter to 1
 chompCount=1;
//create local variables used in initialization
 float fishStart=50;
 float rockstartx;
 float rockstarty;
//create random fish
 fish1=new Fish(fishStart,fishStart,int(random(70,100)),int(random(70,100)),1);
 fishStart=950;
 fish2=new Fish(fishStart,fishStart-900,int(random(70,100)),int(random(70,100)),2);
 fishStart=350;
 fish3=new Fish(fishStart-300,fishStart,int(random(70,100)),int(random(70,100)),3);
 fishStart=500;
 biggie=new Fish(fishStart,fishStart-300,int(random(95,120)),int(random(80,110)),4);
//create random rocks
 rockstartx=int(random(800));
 rockstarty=int(random(400));
 rock1=new Rock(int(rockstartx),int(rockstarty),int(random(30,50)),int(random(30,50)));
 rockstartx=int(random(800));
 rockstarty=int(random(400));
 rock2=new Rock(int(rockstartx),int(rockstarty),int(random(30,50)),int(random(30,50)));
 rockstartx=int(random(800));
 rockstarty=int(random(400));
 big1=new Rock(int(rockstartx),int(rockstarty),int(random(95,120)),int(random(95,120)));
 rockstartx=int(random(800));
 rockstarty=int(random(400));
 small1=new Rock(int(rockstartx),int(rockstarty),int(random(10,30)),int(random(10,30)));
rockstartx=int(random(800));
 rockstarty=int(random(400));
 small2=new Rock(int(rockstartx),int(rockstarty),int(random(10,30)),int(random(10,30)));
rockstartx=int(random(800));
 rockstarty=int(random(400));
 small3=new Rock(int(rockstartx),int(rockstarty),int(random(10,30)),int(random(10,30)));
}

//animation loop
void draw() {
//clear screen
 background(255);
//move and refresh fish
 fish1.move();
 fish1.display();
 fish2.move();
 fish2.display();
 fish3.move();
 fish3.display();
 biggie.move();
 biggie.display();
//re-draw rocks
 rock1.display();
 rock2.display();
 big1.display();
 small1.display();
 small2.display();
 small3.display();
}

class Fish {
//initialize fish parameters
 int ident;
 float x;
 float y;
 int count=0;
 float xmov=0;
 float ymov=0;
 PShape f;
 int rota;
 float rad;
 int sizex;
 int sizey;
 int origsizex;
 int origsizey;
 boolean v;
//create fish based on input
 Fish(float tempX, float tempY,int tempsix,int tempsiy,int tempid)  {
   ident=tempid;
   x = tempX;  
   y = tempY;
   sizex=tempsix;
   sizey=tempsiy;
   origsizex=sizex;
   origsizey=sizey;
   v=false;
//load starting sprite
   f=loadShape("fishthing.svg");
   smooth();
 }
 void move() {
   float chase;
//change the directions once every 30 frames, 1st, 31st, etc.
   if (count==0) {
xmov=random(-2.5,2.5);
ymov=random(-2.5,2.5);
v=false;
   }
   count=count+1;
   if (count==30) {
count=0;
   }
//grow the fish if it is a baby
   if(sizex<origsizex) {
sizex=sizex+1;
   }
   if(sizey<origsizey) {
sizey=sizey+1;
   }
//move the fish's location
   x=x+xmov;
   y=y+ymov;
//check for collision against rocks
   if(x+sizex<=rock1.sizex+rock1.x || x>=rock1.x && y+sizey<=rock1.sizey+rock1.y || y>=rock1.y) {
xmov=-1*xmov;
ymov=-1*ymov;
   }
   if(x+sizex<=rock2.sizex+rock2.x || x>=rock2.x && y+sizey<=rock2.sizey+rock2.y || y>=rock2.y) {
xmov=-1*xmov;
ymov=-1*ymov;
   }
   if(x+sizex<=small1.sizex+small1.x || x>=small1.x && y+sizey<=small1.sizey+small1.y || y>=small1.y) {
xmov=-1*xmov;
ymov=-1*ymov;
   }
   if(x+sizex<=small2.sizex+small2.x || x>=small2.x && y+sizey<=small2.sizey+small2.y || y>=small2.y) {
xmov=-1*xmov;
ymov=-1*ymov;
   }
   if(x+sizex<=small3.sizex+small3.x || x>=small3.x && y+sizey<=small3.sizey+small3.y || y>=small3.y) {
xmov=-1*xmov;
ymov=-1*ymov;
   }
   if(x+sizex<=big1.sizex+big1.x || x>=big1.x && y+sizey<=big1.sizey+big1.y || y>=big1.y) {
xmov=-1*xmov;
ymov=-1*ymov;
   }
//check for fish within an area, and chase/run away
   if(ident!=1) {
if(x+sizex<=fish1.sizex+fish1.x+25 || x>=fish1.x-25 && y+sizey<=fish1.sizey+fish1.y+25 || y>=fish1.y-25) {
 if(sizex*sizey>fish1.sizex*fish1.sizey+30) {
   chase=random(.5,1.5);
   xmov=chase*fish1.xmov;
   ymov=chase*fish1.xmov;
   
 }
}
if(sizex*sizey<fish1.sizex*fish1.sizey-30) {
 if(x>fish1.x && xmov<0 || x<fish1.x && xmov>0) {
   xmov=-1.2*xmov;
   ymov=-1.2*ymov;
 } else {
   xmov=xmov*1.2;
   ymov=ymov*1.2;
 }
}
   }
   if(ident!=2) {
if(x+sizex<=fish2.sizex+fish2.x+25 || x>=fish2.x-25 && y+sizey<=fish2.sizey+fish2.y+25 || y>=fish2.y-25) {
 if(sizex*sizey>fish2.sizex*fish2.sizey+30) {
   chase=random(.5,1.5);
   xmov=chase*fish2.xmov;
   ymov=chase*fish2.xmov;
 }
 if(sizex*sizey<fish2.sizex*fish2.sizey-30) {
   if(x>fish2.x && xmov<0 || x<fish2.x && xmov>0) {
xmov=-1.2*xmov;
ymov=-1.2*ymov;
   } else {
xmov=xmov*1.2;
ymov=ymov*1.2;
   }
 }
}
   }
   if(ident!=3) {
if(x+sizex<=fish3.sizex+fish3.x+25 || x>=fish3.x-25 && y+sizey<=fish3.sizey+fish3.y+25 || y>=fish3.y-25) {
 if(sizex*sizey>fish3.sizex*fish3.sizey+30) {
   chase=random(.5,1.5);
   xmov=chase*fish3.xmov;
   ymov=chase*fish3.xmov;
 }
 if(sizex*sizey<fish3.sizex*fish3.sizey-30) {
   if(x>fish3.x && xmov<0 || x<fish3.x && xmov>0) {
xmov=-1.2*xmov;
ymov=-1.2*ymov;
   } else {
xmov=xmov*1.2;
ymov=ymov*1.2;
   }
 }
}
   }
   if(ident!=2) {
if(x+sizex<=biggie.sizex+biggie.x+25 || x>=biggie.x-25 && y+sizey<=biggie.sizey+biggie.y+25 |
Re: Strange NullPointerException Error, Please Help!
Reply #1 - Apr 21st, 2009, 4:12am
 
Code:
//Fish by Jonah C., Part II

| y>=biggie.y-75) {
 if(sizex*sizey>biggie.sizex*biggie.sizey+30) {
   biggie.v=true;
   biggie.x=random(1000);
   biggie.y=random(400);
   biggie.sizex=10;
   biggie.sizey=10;
   expand(bites,bites.length+1);
   bites[chompCount]=new Chomp(int(x),int(y));
 }
}
   }
//reset the fish at the opposite edge if it goes offscreen
   if (x>width) {
x=0;
   }
   if (y>height) {
y=0;
   }
   if (x<0) {
x=width;
   }
   if (y<0) {
y=height;
   }
//change sprite for direction
if(xmov>=0) {
 f=loadShape("fishthing2.svg");
}
if(xmov<0) {
 f=loadShape("fishthing.svg");
}
 }
 
 void display() {
//display the fish sprite
   shape(f,x,y,sizex,sizey);
 }
}

class Rock {
//initialize rock variables
 int x;
 int y;
 int sizex;
 int sizey;
 PShape r;
 Rock(int tempx, int tempy, int tsizex, int tsizey)  {
//create rocks with given parameters
   x=tempx;
   y=tempy;
   sizex=tsizex;
   sizey=tsizey;
//load the rock sprite
   r=loadShape("rock.svg");
   smooth();
 }
 void display() {
//display the rock sprite
   shape(r,x,y,sizex,sizey);
 }
}

class Chomp {
//initialize Chomp variables
 int x;
 int y;
 PShape p;
 int count;
 Chomp(int tx,int ty) {
//initialize variables
   x=tx;
   y=ty;
   ******p=loadShape("chomp.svg");
   smooth();
 }
//displays for 30 cycles
 void display() {
   if(count<31) {
shape(p,x,y,20,10);
count=count+1;
   }
 }
}


It gets the error at the line with all the asterisks.
Re: Strange NullPointerException Error, Please Help!
Reply #2 - Apr 21st, 2009, 5:48am
 
Is "chomp.svg" file in the data folder of the sketch?
Re: Strange NullPointerException Error, Please Help!
Reply #3 - Apr 21st, 2009, 2:24pm
 
Oh, wait, the name is "CHOMP.svg"!

thanks!
Page Index Toggle Pages: 1