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.
Page Index Toggle Pages: 1
class (Read 566 times)
class
Nov 19th, 2009, 9:26am
 
Hi,

I'm beginning in processing, and I need to do a basic slider
(those of libraries like ControlP5 are too complicated for me at the moment, and I would to modify styles)

So I started to make one, and I have already problems...

It say me that there is an NullPointer exception

There my code :

Code:

Slider slider1;

void setup(){
 /////////////////////////////////////
 size (500,500);
 background(120);
 /////////////////////////////////////

 Slider slider1 = new Slider(20,20,100,20);
}

void draw(){
 slider1.dessiner();
}

class Slider{
 int sPosX, sPosY;
 int sLargeur, sHauteur;
 //int sMin, sMax;
 //int sInitial;
 //int nom;
 //int valeur;

 Slider(int isPosX, int isPosY, int isLargeur, int isHauteur){
   sPosX = isPosX;
   sPosY = isPosY;
   sLargeur = isLargeur;
   sHauteur = isHauteur;
 }

 void dessiner(){
   noStroke();
   fill(150);
   rect(sPosX,sPosY,sLargeur,sHauteur);
   fill(80);
   rect(sPosX,sPosY,sLargeur,sHauteur);
 }

 /*void bouger(){
  if(mousePressed){
  rect(sPosX,sPosY,mouseX-sPosX,sHauteur);
  }
  }*/
}



if someone can have a look, thank you !
Re: class
Reply #1 - Nov 19th, 2009, 9:36am
 
The problem is in setup.  You globally declare your Slider before setup (as you should) so you don't need to declare a new Slider object when you create/instantiate it.  So do this:

Code:
Slider slider1;

void setup(){
 /////////////////////////////////////
 size (500,500);
 background(120);
 /////////////////////////////////////
 // 'Slider' should NOT be declared here!
 slider1 = new Slider(20,20,100,20);
}


...Otherwise by declaring the Slider again in setup you create a local variable called 'slider1', which exists only inside setup(), and the global 'slider1' never gets created, hence the NullPointerException...

P.S.: You should also move your background(120) call from setup to draw() Wink
Re: class
Reply #2 - Nov 19th, 2009, 9:46am
 
thanks a lot !
I think it will not my last question, but it permise me to continue

thank you
Page Index Toggle Pages: 1