# Why NullPointerException

**URL:** <https://discourse.processing.org/t/why-nullpointerexception/36537>\
**Category:** Coding Questions\
**Tags:** homework\
**Created:** [April 23, 2022, 6:42am UTC](https://discourse.processing.org/t/why-nullpointerexception/36537 "2022-04-23T06:42:38Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jiajia](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jiajia/32/16099_2.png) [@jiajia](https://discourse.processing.org/u/jiajia)\
**Post date:** [April 23, 2022, 6:42am UTC](https://discourse.processing.org/t/why-nullpointerexception/36537/1 "2022-04-23T06:42:38Z")

</div>

```auto
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;
    }
  }
}

```

 ![屏幕截图 2022-04-23 143629](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/4/9/499917d031b3a7a9219c04cd2b88cce9bf5a8b6a.jpeg)

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [April 23, 2022, 7:00am UTC](https://discourse.processing.org/t/why-nullpointerexception/36537/2 "2022-04-23T07:00:43Z")

</div>

> [@jiajia](#):
>
> `PVector velocity;`

`PVector velocity = new PVector();`

---

<div class="post-metadata">

**Author:** ![jiajia](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jiajia/32/16099_2.png) [@jiajia](https://discourse.processing.org/u/jiajia)\
**Post date:** [April 23, 2022, 7:55am UTC](https://discourse.processing.org/t/why-nullpointerexception/36537/3 "2022-04-23T07:55:45Z")

</div>

OH,Your help made me instantly enlightened.very thanks. 🙂
