# int96:NullPointerException

**URL:** <https://discourse.processing.org/t/int96-nullpointerexception/40653>\
**Category:** Coding Questions\
**Created:** [January 20, 2023, 4:40am UTC](https://discourse.processing.org/t/int96-nullpointerexception/40653 "2023-01-20T04:40:56Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![int96](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/int96/32/17850_2.png) [@int96](https://discourse.processing.org/u/int96)\
**Post date:** [January 20, 2023, 4:40am UTC](https://discourse.processing.org/t/int96-nullpointerexception/40653/1 "2023-01-20T04:40:56Z")

</div>

So here is the basic code… for the life of me I can’t see whats wrong.

**sketch**

```auto
Particle p;
Particle[] particles = new Particle[1];

void setup() {
  size(200, 200);
  particles[0] = new Particle();
}

void draw() {
  background(255);
  p.update();
  p.show();
}

```

**class**

```auto
class Particle {
  PVector pos;
  PVector vel;
  PVector acc;

  Particle() {
    pos = new PVector(0, 0);
    vel = new PVector(0, 0);
    acc = new PVector(0, 0);
  }

  void applyForce(PVector force) {
    acc.add(force);
  }

  void update() {
    pos.add(acc);
    pos.add(vel);
    acc.mult(0);
  }

  void show() {
    stroke(0);
    point(pos.x, pos.y);
  }
}

```

---

<div class="post-metadata">

**Author:** ![svan](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@svan](https://discourse.processing.org/u/svan)\
**Post date:** [January 20, 2023, 5:11am UTC](https://discourse.processing.org/t/int96-nullpointerexception/40653/2 "2023-01-20T05:11:01Z")

</div>

Particle p was never initialized so you can leave it out; that’s what has the null pointer and is causing the error. Then use a draw() that looks like this to draw array elements:

```auto
void draw() {
  background(255);
  for(int i = 0; i < particles.length; i++){
    particles[i].update();
    particles[i].show();
  }
}

```

You only set up the array for one particle, so if you look closely you’ll see the lone point at 0,0.

Welcome to the forum.

---

<div class="post-metadata">

**Author:** ![int96](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/int96/32/17850_2.png) [@int96](https://discourse.processing.org/u/int96)\
**Post date:** [January 20, 2023, 5:23am UTC](https://discourse.processing.org/t/int96-nullpointerexception/40653/3 "2023-01-20T05:23:06Z")

</div>

**face palm**  
Thank you!! I couldn’t see it!!  
Thanks for the welcome.

---

<div class="post-metadata">

**Author:** ![Mikey83](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/mikey83/32/17956_2.png) [@Mikey83](https://discourse.processing.org/u/Mikey83)\
**Post date:** [January 20, 2023, 5:53am UTC](https://discourse.processing.org/t/int96-nullpointerexception/40653/4 "2023-01-20T05:53:28Z")

</div>

Another way of doing it while using p is

```auto
for (Particle p : particles){
   p.update();
   p.show();
}
```

---

<div class="post-metadata">

**Author:** ![int96](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/int96/32/17850_2.png) [@int96](https://discourse.processing.org/u/int96)\
**Post date:** [January 20, 2023, 6:05am UTC](https://discourse.processing.org/t/int96-nullpointerexception/40653/5 "2023-01-20T06:05:33Z")

</div>

Ahhh thanks I’ll have to check that out.
