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 › null pointer exception ---- cant figure it out
Page Index Toggle Pages: 1
null pointer exception ---- cant figure it out (Read 1391 times)
null pointer exception ---- cant figure it out
Oct 18th, 2009, 3:23pm
 
i keep getting a null pointer exception and cant figure out why. i'm trying to call an audioplayer object from a custom function. here is my code any help would be appreciated  Smiley

Code:
import ddf.minim.*;

AudioPlayer player;
Minim minim;

AudioPlayer player2;
Minim minim2;



Ball[] balls = new Ball[150];
float[] ballsx = new float[150];
float[] ballsy = new float[150];

boolean COonce = true;


void setup(){
 size(960,600);
 smooth();

 float tempx = 48;
 float tempy = height/2;
 
 for(int i = 0; i < balls.length; i++){
   ballsx[i] = random(25,935);
   ballsy[i] = random(25,575);
     //tempx += 96;
   balls[i] = new Ball(ballsx[i],ballsy[i],50);
 }
  checkoverlap();

 minim = new Minim(this);
 player = minim.loadFile("gong_d4.wav");
 
 minim2 = new Minim(this);
 player2 = minim2.loadFile("gong_d4.wav");


}
void draw(){

 background(0);

 for(int i = 0; i < balls.length; i++){
   balls[i].display();
 }
 
 checkoverlap();
 
 for(int i = 0; i < balls.length; i++){

   if(mouseX > balls[i].ballx-balls[i].balldia/2 && mouseX < balls[i].ballx+balls[i].balldia/2 && mouseY > balls[i].bally-balls[i].balldia/2 && mouseY < balls[i].bally+balls[i].balldia/2 && mousePressed){
     balls[i].ballx = mouseX;
     balls[i].bally = mouseY;
     balls[i].mouse = true;
     player.play();
     

   }
   else if(!mousePressed){
     balls[i].mouse = false;
     //minim.stop();
   }
 }
 
}

void mouseReleased(){
 //player.close();
 //minim.stop();
 //player = minim.loadFile("gong_d4.wav");
}

void checkoverlap(){
 for(int i = 0; i < ballsx.length; i++){
   for(int x = 1; x < ballsx.length; x++){
     //println((sqrt(sq(balls[i].ballx-balls[x].ballx)+sq(balls[i].bally-balls[x].bally))));
     if((sqrt(sq(balls[i].ballx-balls[x].ballx)+sq(balls[i].bally-balls[x].bally))) < 50){
       
       balls[i].ballx+=2;
       balls[i].bally+=2;
       balls[x].ballx-=2;
       balls[x].bally-=2;
       
       player2.play();
     }
   }
 }
}

void checkcollision(){
 
}
class Ball{
 float ballx;
 float bally;
 float balldia;
 boolean mouse;
 
 Ball(float x, float y, float dia){
   ballx = x;
   bally = y;
   balldia = dia;
   mouse = false;

 }

 void display(){
   if(mouse == false){
     stroke(100,100,100);
   }
   else if(mouse == true){
     stroke(255);
   }
   fill(0);
   ellipseMode(CENTER);
   ellipse(ballx, bally, 50,50);
 }
 
}
Re: null pointer exception ---- cant figure it out
Reply #1 - Oct 18th, 2009, 11:44pm
 
which line is giving the null pointer exception?
Re: null pointer exception ---- cant figure it out
Reply #2 - Oct 18th, 2009, 11:48pm
 
you're calling checkOverlap() in setup (which references player2) before initialising player2.
Re: null pointer exception ---- cant figure it out
Reply #3 - Oct 20th, 2009, 11:43am
 
thanks that was it Smiley
Page Index Toggle Pages: 1