I don't get what I'm doing wrong, I was just trying to make a simple arraylist
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;
}
}