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 463 times)
NullPointerException ?
Dec 13th, 2008, 1:14am
 
Hi there !

Still working on my li'll project and still getting lot's of errors, thanks to your help, I went throw a lot of them (and my code get lighter ^^), but there still new incomers -_-

so here it is, the new one : I get a NullPointException error on the boule[0].x=100 (and I think it's the same for all the other boule[0,1].y,r,m etc... above it). I looked for other thread about this error but didn't manage to find the solution in this case, so If you have some time, could you have a look please ?

Here is the code (if neeeded I can also display the boule class code, I don't know if it can be related to)

Code:

Boule[] boules = new Boule[20];


int width = 400 ;
int height = 400 ;


float gravite;



void setup() {
 size(width,height);
 smooth();
 
 boules[0].x=100;
 boules[1].x=150;
 
 boules[0].y=150;
 boules[1].y=220;
 
 boules[0].r=50;
 boules[1].r=12;
 
 boules[0].m=7;
 boules[1].m=1;
 

 

 gravite = .015;
}

void draw() {
 background(255);
}
 
void create(){
 for(int i=0; i<boules.length; i++){
    boules[i] = new Boule(boules[i].r,boules[i].x,boules[i].y,boules[i].m,i);
 }
}
 
void bounce(){
 for(int i=0; i<boules.length; i++){
    boules[i].action();
 
}
}
Re: NullPointerException ?
Reply #1 - Dec 13th, 2008, 2:10am
 
please post your Boule class as well.
Re: NullPointerException ?
Reply #2 - Dec 13th, 2008, 2:12am
 
actually just try this:

change
Boule[] boules = new Boule[20];
to
Boule[] boules;

and add in setup() this:
boules = new Boule[20];
Re: NullPointerException ?
Reply #3 - Dec 13th, 2008, 1:55pm
 
Some remarks:
- size() recommends to use literal numbers in its call, so when exporting, Processing will know which size to use.
- If you do the above, you will have width and height variables automatically available and equal to the values you set in the size() call.
- As rebirth said, it is in general better to separate array declaration and setup() (not sure why, in Java it is OK).
- You do the initialization of the array correctly in the create() function, but you forgot to call it before using boules[]! Wink I think that's the main culprit.
Re: NullPointerException ?
Reply #4 - Dec 13th, 2008, 6:04pm
 
Hi there ! thanks for your help Smiley

I modified the main code, in fact the create() wasn't needed I think (cause I already got a similar fonction in the class code) so I removed it for the draw part, and put a part of it in the setup to initialize correctly values, did I do it in the right way ? )
also, I modified my array declaration as you told me, but I still got the same error (NullPointerException) so I'm a bit lost °-° , anyway here the code :

Main Part :

Code:
Boule[] boules;



float gravite;



void setup() {
size(400,400);
smooth();
boules = new Boule[20];

boules[0].x=100;
boules[1].x=150;

boules[0].y=150;
boules[1].y=220;

boules[0].r=50;
boules[1].r=12;

boules[0].m=7;
boules[1].m=1;

for(int i=0; i<boules.length; i++){
boules[i] = new Boule(boules[i].r,boules[i].x,boules[i].y,boules[i].m,i);
}

gravite = .015;
}

void draw() {
background(255);
for(int i=0; i<boules.length; i++){
boules[i].action();

}
}



and Boule class Part :

Code:
class Boule {

float r; // rayon
float x,y; // position

int index;

color c = color(0,0);

float xv = 0; //vélocité actuelle le long de l'axe des x
float yv = 0; //vélocité actuelle le long de l'axe des y

float xd;
float yd;
float d;

float gAngle;
float gTheta; //angle en radians
float gxv; // gravité en x
float gyv; // gravité en y
float m; // masse particule

Boule(float r_init, float x_init, float y_init, float m_init, int i_init) {
x = x_init;
y = y_init;
r = r_init;

index = i_init;

m = m_init;
}

void action(){
appliquerGravite();
bouge();
rebondir_ecran();
allume();
dessine();
}



void appliquerGravite(){
for(int i=0; i<boules.length; i++){
if (i != index ){
gAngle = calculAngle(x, y, boules[i].x, boules[i].y);
gTheta = (-(gAngle * PI))/180;
gxv = cos(gTheta) * (-gravite/(r/6));
gyv = sin(gTheta) * (-gravite/(r/6));


boules[i].xv += m*boules[i].gxv;
boules[i].yv += m*boules[i].gyv;
xv += m*gxv;
yv += m*gyv;
}
}
}

void bouge(){

x += xv;
y += yv;

}

void rebondir_ecran(){

if (x > width-50){
xv = xv-(4/r);}
if(x < 50){
xv = xv+(4/r);}

if (y > height-50){
yv = yv-(4/r);}
if(y < 50){
yv = yv+(4/r);}
}



void allume (){
for(int i=0; i<boules.length; i++){
if (i != index ){

c = color(150,50);
xd = x - boules[i].x;
yd = y - boules[i].y;
}
}
}



void dessine () {

noStroke();
fill(c);
ellipse(x,y,r*2,r*2);
c = color(0,0,0,128);
}

float calculDistance(float x1, float y1, float x2, float y2){
xd = x1 - x2;
yd = y1 - y2;
d = sqrt(xd * xd + yd * yd);
return d;

}


float calculAngle ( float x1, float y1, float x2, float y2){
float xd = x1 - x2 ;
float yd = y1 - y2 ;

float t = atan2 (yd,xd);
float a =(180 + (-(180*t) / PI));
return a;
}
}
Re: NullPointerException ?
Reply #5 - Dec 14th, 2008, 10:22am
 
You access boules[0] before initializing it... Put the for loop just after the init of the array (new Boule[]).
Re: NullPointerException ?
Reply #6 - Dec 14th, 2008, 11:09pm
 
thanks for your answer ^^

I did just as you said, so now it's like this :

Code:

Boule[] boules;


float gravite;



void setup() {
 size(400,400);
 smooth();
 boules = new Boule[20];
 
  for(int i=0; i<boules.length; i++){
 boules[i] = new Boule(boules[i].r,boules[i].x,boules[i].y,boules[i].m,i);
 }
 
 boules[0].x=100;
 boules[1].x=150;
 
 boules[0].y=150;
 boules[1].y=220;
 
 boules[0].r=50;
 boules[1].r=12;
 
 boules[0].m=7;
 boules[1].m=1;
 

 
 gravite = .015;
}

void draw() {
 background(255);
 for(int i=0; i<boules.length; i++){
    boules[i].action();
 
}
}


but I still got a NullPointerException °_° but the error moved to this line :

Code:
boules[i] = new Boule(boules[i].r,boules[i].x,boules[i].y,boules[i].m,i); 



Am I cursed XD ?

in fact, what does mean the NullPointerException ?

edit :

finally I managed to make it work, but by removing some controls. I removed all the way to control values of each Boule object by doing this :

Code:
Boule[] boules;



float gravite;



void setup() {
 size(400,400);
 smooth();
 boules = new Boule[5];
 
  for(int i=0; i<boules.length; i++){
    float dr = random(5,50);
    float dx = random(width);
    float dy = random(height);
    float dm = random(1,10);
 boules[i] = new Boule(dr,dx,dy,dm,i);
 }
 

 

 
 gravite = .015;
}

void draw() {
 background(255);
 for(int i=0; i<boules.length; i++){
    boules[i].action();
 
}
}


but now I got to find a way to put back correctly controls on Boule object like boule[i].x = 100 etc... for the moment I didn't find out how to do it, and get back NullPointerException, Do You have any idea of the way to do it properly ?

for example, by restarting on the working version I tried this way but get back NullPointerException on the
Code:
float dr = boules[i].r; 



it seems that processing don't like boule[i]."some variable", but I don't know why.

here is it :
Code:
Boule[] boules;


float gravite;



void setup() {
size(400,400);
smooth();
boules = new Boule[2];

for(int i=0; i<boules.length; i++){
float dr = boules[i].r;
float dx = boules[i].x;
float dy = boules[i].y;
float dm = boules[i].m;

boules[0].r = 10;
boules[0].x = 100;
boules[0].y = 10;
boules[0].m = 3;

boules[1].r = 25;
boules[1].x = 250;
boules[1].y = 300;
boules[1].m = 3;

boules[i] = new Boule(dr,dx,dy,dm,i);
}





gravite = .015;
}

void draw() {
background(255);
for(int i=0; i<boules.length; i++){
boules[i].action();

}
}
Re: NullPointerException ?
Reply #7 - Dec 15th, 2008, 1:37am
 
I did it !!! I find out a way to do it, in fact (like I tried to do at the beginning) I created arrays also for x,r,y and m values, but now, with your advices I remade it properly ,so now it works!! (don't worry, my project is far to be finished so I think that in several days I'll comeback with a new problem XD) So Thanks a lot everyone Cheesy !

here is it :

Code:

Boule[] boules;

float[] x = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
float[] y = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
float[] r = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
float[] m = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};


float gravite;



void setup() {
 size(400,400);
 smooth();
 boules = new Boule[2];
 
 r[0] = 10;
 x[0] = 100;
 y[0] = 10;
 m[0] = 3;
   
 r[1] = 25;
 x[1] = 250;
 y[1] = 300;
 m[1] = 3;
 
 
  for(int i=0; i<boules.length; i++){
    float dr = r[i];
    float dx = x[i];
    float dy = y[i];
    float dm = m[i];
   
    boules[i] = new Boule(dr,dx,dy,dm,i);
 }
 

 

 
 gravite = .015;
}

void draw() {
 background(255);
 for(int i=0; i<boules.length; i++){
    boules[i].action();
 
}
}


just a last question about this , is there a way to not write {0,0,0,0,0,0,0,0,0,0,0....} to initialize values?
Re: NullPointerException ?
Reply #8 - Dec 15th, 2008, 1:50pm
 
Ah, I was about to advise not to init your arrays this way, before seeing your question... :-D

Simple way: float[] x = new float[20]; // Adjust value, of course
By default, arrays of primitive values (int, long, float, double, boolean, char...) are initialized to 0 (false for booleans).

And to answer the second question: a NPE (NullPointerException) happens frequently. Particularly when you use a method or access a field of an object which hold a null value (ie. isn't set):
foo.x or foo.getX() will throw such exception because foo is null, and thus you cannot access anything in it.
Re: NullPointerException ?
Reply #9 - Dec 16th, 2008, 12:37am
 
ok thanks for that Smiley !
Page Index Toggle Pages: 1