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 › Simplest arrayList not working: "Thread-2&
Page Index Toggle Pages: 1
Simplest arrayList not working: "Thread-2& (Read 353 times)
Simplest arrayList not working: "Thread-2&
Mar 7th, 2008, 2:52am
 
I don't get what I'm doing wrong, I was just trying to make a simple arraylist Sad

I get an:
Exception in thread "Thread-2" java.lang.NullPointerException
     at Temporary_6124_5011.run(Temporary_6124_5011.java:30)




Code:

ArrayList particles;

void setup(){
size(200,200); smooth();

particles = new ArrayList(); // Initialize the arraylist
for (int i = 0; i < 15; i++) particles.add(new Particle(random(width), random(height)));
println(particles.size()+" new particles");
}



void draw() {
background(0);
run();
addParticle(mouseX,mouseY);

fill(200);
rect(0,0,5,particles.size());
}


//this is where I suspect the problem is
void run() {
// Cycle through the ArrayList backwards b/c we are deleting
// I got this from the official example
for (int i = particles.size()-1; i >= 0; i--) {
Particle p = (Particle) particles.get(i);
p.run();
if (p.dead()) {
particles.remove(i);
}
}
}


void addParticle(float _x, float _y) {
particles.add(new Particle(_x, _y));
}

boolean dead() {
if (particles.isEmpty()) return true;
else return false;
}





there, easy. nd the particle code is pretty basic, just to test this out


Code:

class Particle {
float r;
float timer;
float x,y;

Particle(float _x, float _y) {
x = _x;
y = _y;
r = 10.0;
timer = random(50,130);
}

void run() {
update();
render();
}

// update location
void update() {
x+=random(-1,1);
y+=random(-1,1);
timer -= 1;
}

void render() {
ellipseMode(CENTER);
noStroke();
fill(255,timer);
ellipse(x,y,r,r);
}

boolean dead() {
if (timer <= 0) return true;
else return false;
}


}


Re: Simplest arrayList not working: "Thread-2
Reply #1 - Mar 7th, 2008, 9:04pm
 
It seems not a good idea to name the method run() (I don't know why). Changing the name to runner() fix this problem. By the way, instead of an for loop you can use an iterator and remove() to kill your particle:

Code:

Iterator iter = particles.iterator();
while (iter.hasNext()) {

Particle p = (Particle) iter.next();

p.run();

if (p.dead()) {


iter.remove();

}
}
Re: Simplest arrayList not working: "Thread-2
Reply #2 - Mar 11th, 2008, 9:58pm
 
Cheers!

I solved the run() problem myself, and I just put your more elegant solution in.

thx
Re: Simplest arrayList not working: "Thread-2
Reply #3 - Mar 12th, 2008, 8:04am
 
the main animation loop uses run(), so you shouldn't have another function with the same name, otherwise your code won't work.
Page Index Toggle Pages: 1