I'm guessing you're getting a NullPointerException.
If the current second (i.e., the return value from second()) is even, s will be 0. Since s is then equal to s0, createOrb() is not called before you reach the for loop. That means the circles variable still has its initial value of null. When you try to get the length of null, you get a NullPointerException.
A minimal code change is to wrap the for loop in a check for null:
if (circles != null) {
}
But, then it seems that append doesn't like to work on empty arrays.
If you try to run:
String[] strArray;
String str = "foo";
strArray = (String[]) append(strArray, str);
you get the message "The local variable strArray may not have been initialized." If you explicitly initialize it to null:
String[] strArray = null;
You get an NPE.
Personally, I prefer Java collections. I added a Circle class to your code so I could run it. I added comments before the lines I changed or added.
Quote:int so = 0;
int interval = 2;
int numCircles = 20;
Circle circle;
// Create an ArrayList to hold the Circles
ArrayList circles = new ArrayList();
class Circle {
int radius;
Circle(int radius) {
this.radius = radius;
}
void display() {
}
void move() {
}
}
void setup() {
size(640, 480);
smooth();
noStroke();
background(0);
}
void draw() {
int s = second() % interval;
if (s != so) {
so = s;
if (s == interval - 1) {
println("add object");
createOrb();
}
}
// Check for null
if (null != circles) {
for (int n = 0; n < circles.size(); n++) {
// Display and move each Circle
Circle circle = (Circle) circles.get(n);
circle.display();
circle.move();
}
}
}
void createOrb() {
circle = new Circle(int(random(40)));
// Add new Circle to the ArrayList
circles.add(circle);
}