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 › Array rookie question
Page Index Toggle Pages: 1
Array rookie question (Read 586 times)
Array rookie question
Dec 29th, 2008, 1:07pm
 
Hi, here is the problem, I am trying to add objects on a time interval, wich is working fine, but as they are created I want to store them into an array so I can do stuff with them, and here is where I fail, I dont know how to add elements to the array as they are created.

I create the array and them I think I need to use append to add new elements to the array, but I dont know, I have actionscript background so I used to use array,push().

Here is a sample code:

// INI SAMPLE CODE

int so = 0;
int interval = 2;
int numCircles = 20;
Circle circle;
Circle[] circles;


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();
   }
 }
 
 for (int n = 0; n < circles.length; n++) {
   circles[n].display();
   circles[n].move();
 }
}

void createOrb() {
 circle = new Circle(int(random(40)));
 circles = append(circles, circle);
}

// END SAMPLE CODE

I get the folloing error "cannot convert from Object to IntervalTest.Circle[]" on
the last line of code.

Many thanks!
rS

Re: Array rookie question
Reply #1 - Dec 29th, 2008, 2:17pm
 
circles = append(circles, circle);  should be circles = (Circle[])append(circles, circle);
Re: Array rookie question
Reply #2 - Dec 29th, 2008, 2:53pm
 
Thanks JohnG,

now I got an error on the line

for (int n = 0; n < circles.length; n++) {

any ideas?
rS
Re: Array rookie question
Reply #3 - Dec 29th, 2008, 3:30pm
 
1) What error
2) For such usage (lot of appends, using objects instead of primitives), I would look at ArrayList, more efficient. You would do circles.add(circle);, close of your push().
Re: Array rookie question
Reply #4 - Jan 2nd, 2009, 9:41am
 
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);
}



Page Index Toggle Pages: 1