Why NullPointerException

import java.util.Iterator;

ParticleSystem ps;
Particle p;

void setup(){
  size(200,200);
  smooth();
  ps = new ParticleSystem(new PVector(width/2,50));
}

void draw(){
  background(100);
  PVector gravity = new PVector(0,0.1);
  ps.applyForce(gravity);
  ps.addParticle();
  ps.run();
}

class ParticleSystem{
  ArrayList<Particle>particles;
  PVector origin;
  
  ParticleSystem(PVector location){
    origin = location.copy();
    particles = new ArrayList<Particle>();
  }
  
  void addParticle(){
    particles.add(new Particle(origin));
  }

   void applyForce(PVector f){
     for(Particle p : particles){
       p.applyForce(f);
     }
   }
   
   void run(){
     Iterator<Particle>it = particles.iterator();
     while(it.hasNext()){
       Particle p = (Particle)it.next();
       p.run();
       if(p.isDead()){
         it.remove();
       }
     }
   }
}
  
class Particle{
  PVector location;
  PVector velocity;
  PVector acceleration;
  float lifespan;
  
  float mass = 1;
  
  Particle(PVector l){
    acceleration = new PVector(random(-1,1),random(-2,0));
    location = l.copy();
    lifespan = 255.0;
  }
  
  void run(){
    update();
    display();
  }
  
  void applyForce(PVector force){
    PVector f = force.copy();
    f.div(mass);
    acceleration.add(f);
  }
  
  void update(){
    velocity.add(acceleration);
    location.add(velocity);
    acceleration.mult(0);
    lifespan-=2.0;
  }
  
  void display(){
    stroke(255,lifespan);
    fill(255,lifespan);
    ellipse(location.x,location.y,8,8);
  }
  
  boolean isDead(){
    if(lifespan<0.0){
      return true;
    }else{
      return false;
    }
  }
}

PVector velocity = new PVector();

2 Likes

OH,Your help made me instantly enlightened.very thanks. :slightly_smiling_face:

1 Like