# The Particle() is undefined

**URL:** https://discourse.processing.org/t/the-particle-is-undefined/36510
**Category:** Beginners
**Tags:** homework
**Created:** [April 22, 2022, 3:55am UTC](https://discourse.processing.org/t/the-particle-is-undefined/36510 "2022-04-22T03:55:24Z")
**Posts on this page:** 8
**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 22, 2022, 3:55am UTC](https://discourse.processing.org/t/the-particle-is-undefined/36510/1 "2022-04-22T03:55:24Z")

</div>

```auto
import java.util.Iterator;

ParticleSystem ps;
Particle p;

void steup(){
  size(200,200);
  ps = new ParticleSystem();
}
void draw(){
  background(255);
  ps.run();
}

class ParticleSystem{
  ArrayList<Particle> particles;
  
  ParticleSystem(){
    particles = new ArrayList<Particle>();
  }
  
  void addParticle(){
    particles.add(new Particle());
  }
  void run(){
    Iterator<Particle>it = particles.iterator();
    while(it.hasNext()){
      Particle p = it.next();
      p.run();
      if(p.isDead()){
        it.remove();
      }
    }
  }
}

class Particle{
  PVector location;
  PVector velocity;
  PVector acceleration;
  float lifespan;
  
  Particle(PVector l){
    acceleration = new PVector(0,0.05);
    velocity = new PVector(random(-1,1),random(-2,0));
    location = l.copy();
    lifespan = 255.0;
  }

  void run(){
    update();
    display();
  }
  
  void update(){
    velocity.add(acceleration);
    location.add(velocity);
    lifespan -= 2.0;
  }
  
  void display(){
    stroke(0,lifespan);
    fill(0,lifespan);
    ellipse(location.x,location.y,8,8);
  }
  
  boolean isDead(){
    if(lifespan < 0.0){
      return true;
    }else{
      return false;
    }
  }
}

```

 ![屏幕截图 2022-04-22 115155](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/a/7/a7f379b94d4af5c40027030f35962b8b8249667c.jpeg)

---

<div class="post-metadata">

### Author: ![SomeOne](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/someone/32/8639_2.png) [@SomeOne](https://discourse.processing.org/u/SomeOne)
#### Post date: [April 22, 2022, 4:12am UTC](https://discourse.processing.org/t/the-particle-is-undefined/36510/2 "2022-04-22T04:12:45Z")

</div>

Yes it is undefined. You have defined Particle constructor with PVector parameter `Particle(PVector l){`, but you haven’t defined constructor without parameters `Particle(){` as you try to use it.

---

<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 22, 2022, 4:55am UTC](https://discourse.processing.org/t/the-particle-is-undefined/36510/3 "2022-04-22T04:55:30Z")

</div>

OH.I was negligent about this and your help allowed me to solve the error in an instant.very thanks 🙂

---

<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 22, 2022, 5:02am UTC](https://discourse.processing.org/t/the-particle-is-undefined/36510/4 "2022-04-22T05:02:50Z")

</div>

A little problem arose and I asked you again.

 ![屏幕截图 2022-04-22 125939](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/3/f/3fcd1c471a3f1585551fe1f140e06e6583be5574.jpeg)

---

<div class="post-metadata">

### Author: ![SomeOne](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/someone/32/8639_2.png) [@SomeOne](https://discourse.processing.org/u/SomeOne)
#### Post date: [April 22, 2022, 5:10am UTC](https://discourse.processing.org/t/the-particle-is-undefined/36510/5 "2022-04-22T05:10:09Z")

</div>

You have misspelled `void setup()` and ps is not created before you call it.

---

<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 22, 2022, 7:35am UTC](https://discourse.processing.org/t/the-particle-is-undefined/36510/7 "2022-04-22T07:35:04Z")

</div>

But how do I create it 🙂 🤔

```auto
import java.util.Iterator;

ParticleSystem ps;
Particle p;

void setup(){
  size(400,400);
  ps = new ParticleSystem();
}
void draw(){
  background(255);
  ps.run();
}

class ParticleSystem{
  ArrayList<Particle> particles;
  
  ParticleSystem(){
    particles = new ArrayList<Particle>();
  }
  
  void addParticle(){
    particles.add(new Particle());
  }
  void run(){
    Iterator<Particle>it = particles.iterator();
    while(it.hasNext()){
      Particle p = it.next();
      p.run();
      if(p.isDead()){
        it.remove();
      }
    }
  }
}

class Particle{
  PVector location;
  PVector velocity;
  PVector acceleration;
  float lifespan;
  
  Particle(){
    acceleration = new PVector(0,0.05);
    velocity = new PVector(random(-1,1),random(-2,0));
    location = location.copy();
    lifespan = 255.0;
  }

  void run(){
    update();
    display();
  }
  
  void update(){
    velocity.add(acceleration);
    location.add(velocity);
    lifespan -= 2.0;
  }
  
  void display(){
    stroke(0,lifespan);
    fill(0,lifespan);
    ellipse(location.x,location.y,8,8);
  }
  
  boolean isDead(){
    if(lifespan < 0.0){
      return true;
    }else{
      return false;
    }
  }
}

```

---

<div class="post-metadata">

### Author: ![SomeOne](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/someone/32/8639_2.png) [@SomeOne](https://discourse.processing.org/u/SomeOne)
#### Post date: [April 22, 2022, 8:41am UTC](https://discourse.processing.org/t/the-particle-is-undefined/36510/8 "2022-04-22T08:41:37Z")

</div>

> [@jiajia](#):
>
> `ps = new ParticleSystem();`

This creates an empty storage for Particles. To get something going on you need to add Particles the storage. You can add Particle with command `ps.addParticle()`

> [@jiajia](#):
>
> ```auto
> void addParticle(){
> particles.add(new Particle());
> }
> 
> ```

---

<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 22, 2022, 9:57am UTC](https://discourse.processing.org/t/the-particle-is-undefined/36510/9 "2022-04-22T09:57:58Z")

</div>

yea very thankyou，I learning 🙂
