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 › nullpointerexception code test
Page Index Toggle Pages: 1
nullpointerexception code test (Read 421 times)
nullpointerexception code test
Sep 22nd, 2008, 5:37am
 
Trying to learn coding for fun, I get nullpointerexception when I try to compile the code.  First time using Processing also, sorry if my coding is lacking :(  My goal is to be able to create an ellipse with physics and collision on a mouse release.  Thank you in advance :)

int defBalls = 0;
int numBalls = 10;
float spring = 0.05;
float gravity = 0.03;
Ball[] balls = new Ball[numBalls];
int[] y = new int[numBalls];
int[] x = new int[numBalls];
float ranBall = random(10, 40);

void setup()
{
 size(200, 800);
 noStroke();
 smooth();
}

void draw() {
 background(0);
 for (int i = 0; i < numBalls; i++) {
   balls[i].collide();
   balls[i].move();
   balls[i].display();  
 }
}

class Ball {
 float x, y;
 float diameter;
 float vx = 0;
 float vy = 0;
 int id;
 Ball[] others;

 Ball(float xin, float yin, float din, int idin, Ball[] oin) {
   x = xin;
   y = yin;
   diameter = din;
   id = idin;
   others = oin;
 }
 
 void collide() {
   for (int i = id + 1; i < numBalls; i++) {
     float dx = others[i].x - x;
     float dy = others[i].y - y;
     float distance = sqrt(dx*dx + dy*dy);
     float minDist = others[i].diameter/2 + diameter/2;
     if (distance < minDist) {
       float angle = atan2(dy, dx);
       float targetX = x + cos(angle) * minDist;
       float targetY = y + sin(angle) * minDist;
       float ax = (targetX - others[i].x) * spring;
       float ay = (targetY - others[i].y) * spring;
       vx -= ax;
       vy -= ay;
       others[i].vx += ax;
       others[i].vy += ay;
     }
   }  
 }
 
 void move() {
   vy += gravity;
   x += vx;
   y += vy;
   if (x + diameter/2 > width) {
     x = width - diameter/2;
     vx += -0.9;
   }
   else if (x - diameter/2 < 0) {
     x = diameter/2;
     vx *= -0.9;
   }
   if (y + diameter/2 > height) {
     y = height - diameter/2;
     vy *= -0.9;
   }
   else if (y - diameter/2 < 0) {
     y = diameter/2;
     vy *= -0.9;
   }
 }
 
 void display() {
   fill(255);
   ellipse(x, y, diameter, diameter);
 }
}

void mouseReleased() {
   addBall(mouseX, mouseY);
 }

void drawBall(int index) {
 fill(255);
 ellipse(x[index], y[index], ranBall, ranBall);
}

void addBall(int newX, int newY) {
 if (defBalls < numBalls) {
   x[defBalls] = newX;
   y[defBalls] = newY;
   defBalls++;
 }
}
Re: nullpointerexception code test
Reply #1 - Sep 22nd, 2008, 2:31pm
 
it looks like you're missing something like:

Code:

for (int i = 0; i < numBalls; i++) {
int x = int(random(width));
int y = int(random(height));
addBall(x, y);
}


to create the balls inside setup().

(also fyi, when posting code here, you can use Edit > Copy for Discourse, which will put a syntax-colored version on the clipboard that you can paste into your post.)
Re: nullpointerexception code test
Reply #2 - Sep 22nd, 2008, 3:28pm
 
Thank you kind sir, you rock and thank you for being a moderator on the forum.
Page Index Toggle Pages: 1