We are about to switch to a new forum software. Until then we have removed the registration on this forum.
And here I am again, with a code that is not working . I am trying to run this but I keep getting NullPointerException. I guess it is a problem with declaring the array.
Bubble[] myBubble = new Bubble[20];
void setup() {
size(500, 500);
for (int i=0; i< myBubble.length; i++) {
int s = 20;
int x1 = int(random(20, 480));
int y1 = int(random(20, 480));
int z = int(random(3));
color c1;
if ( z>=1) {
c1 = color(255, 0, 0);
} else {
c1= color(0, 0, 255);
}
myBubble [i] = new Bubble( x1, y1, c1, s);
for (int j=0; j< myBubble.length; j++) {
if (dist(myBubble[i].x, myBubble [i].y, myBubble[j].x, myBubble [j].y)< myBubble[i].size/2 ||
dist(myBubble[i].x, myBubble [i].y, myBubble[j].x, myBubble [j].y)< myBubble[j].size/2) {
myBubble[i].x = int(random(20, 480));
myBubble[i].y = int(random(20, 480));
}
}
}
}
void draw() {
background(255);
for (int i = 0; i<myBubble.length; i++) {
myBubble [i] .display();
}
}
Answers
The problem is lines 16 to 22 where you attempt to test the distances between the bubbles but at this stage they have not all been created yet.
thank you !
See Why do I get a NullPointerException?...