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 › weird NullPointerException
Page Index Toggle Pages: 1
weird NullPointerException (Read 857 times)
weird NullPointerException
Jun 30th, 2005, 6:38pm
 
I have this code:

Code:

class BALL {
float xpos, ypos; // position
float vx, vy ;//viteza
float initX, initY;


BALL(int ix, int iy) {
initX = ix;
initY = iy;
init();
}



void init(){
xpos = initX;
ypos = initY;
vx= 4 + random(3); //viteza pe orizontala
if (random(-1,1)<0) vx = -vx;

vy = 5 + random(5); //viteza pe verticala
if (myScore.lastPoint == 1) vy = -vy;

}

void update() {
boolean hit = false;
boolean playerOne=false;
if (ypos < height/2) playerOne = true; // playerOne=true daca bila e in jumatatea de sus

for(int i=0;i<numPads;i++){
if (xpos>pads[i].xpos-padWidth/2 && xpos<pads[i].xpos+padWidth/2 && abs(ypos-pads[i].ypos)<15) { // S-A LOVIT DE UN PAD
if(playerOne) vy = abs(vy); // ii skimb directia, ceva mai shmeker decit vy= -vy
else vy = -abs(vy);
vy *= 1.02;
if (abs(vx/vy)>1) vx *= 0.8; // daca miscarea e prea orizontala, o mai temperez
else vx += random(-3,3); // daca nu, o fut un pic

hit = true;
}
}

if (xpos>width-8 || xpos<8) vx = -vx; //perete lateral
if ( (ypos>height-4 || ypos<12) && !hit) { // s-a dat gol
if (playerOne) {
myScore.p2points++;
myScore.lastPoint = 1;
} else {
myScore.p1points++;
myScore.lastPoint = 2;
}
delay(600);
init();
}

xpos += vx;
ypos += vy;
image(imgBall, round(xpos)-8, round(ypos)-8);
}
}



and this class

Code:

class SCORE {
int p1points=0, p2points=0, limit = 5; // LIMIT e limita pina la care se joaca
int lastPoint = 0; // 0 la initializare, 1 daca ultimul punct cistigat e al lui P1, 2, daca al lui P2
boolean winner=false;

SCORE() {
}

void update(){
if (p1points > limit || p2points > limit){
p1points = 0;
p2points = 0;
winner = true;
} else {
draw(); // desenez scorul si invers, pt amindoi jucatorii ;) ain't I sweet
pushMatrix();
rotate(PI);
translate(-width,-height);
draw();
popMatrix();
}
}

void draw() {
int y = height-100, x=20, spacing=20;
fill(200);
textAlign(LEFT);
text("P1 Points: "+p1points, x, y); y+=spacing;
text("P2 Points: "+p2points, x, y);
}
}



everything's fine if I remove the " if (myScore.lastPoint == 1)    vy = -vy; " part  (last line in INIT() )

All other myScore references, like "myScore.p1points, etc, are ok.  It doesn't seem to like the lastPoint variable.
What's wrng with it ?
Re: weird NullPointerException
Reply #1 - Jun 30th, 2005, 11:28pm
 
Is it possible to see the full code?

When you call init, has the object myScore been set up properly yet? I would presume a the myScore variable would cause the null point exception as apposed to the lastPoint integer.
Re: weird NullPointerException
Reply #2 - Jul 1st, 2005, 5:53pm
 
yeah, I was tired & dumb. I needed to make a score object first, and then init the ball, of course. Thanks.
Page Index Toggle Pages: 1