# I'm getting a NullPointerException

**URL:** <https://discourse.processing.org/t/im-getting-a-nullpointerexception/5529>\
**Category:** Coding Questions\
**Created:** [November 14, 2018, 1:35pm UTC](https://discourse.processing.org/t/im-getting-a-nullpointerexception/5529 "2018-11-14T13:35:36Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Str1XHyper](https://avatars.discourse-cdn.com/v4/letter/s/34f0e0/32.png) [@Str1XHyper](https://discourse.processing.org/u/Str1XHyper)\
**Post date:** [November 14, 2018, 1:35pm UTC](https://discourse.processing.org/t/im-getting-a-nullpointerexception/5529/1 "2018-11-14T13:35:36Z")

</div>

Can someone help me fix this problem. when I run this code I get a NullPointerException at rockets[i].update(), and even if I comment that out I get the same error but on rockets[i].display() and I don’t know why I have tried making it a single object and I have tried putting only one item in the array, but I still can figure it out.

Thanks in advance

```auto
Rockets[] rockets;

void setup() {
  size(1200, 800);
  rockets = new Rockets[1];
}

void draw() {
  background(0);
  for (int i=0; i < rockets.length; i++) {
    rockets[i].update();
    rockets[i].display();
  }
}

public class Rockets
{
  PVector pos;
  PVector vel;
  PVector acc;

  void rockets() {
    this.pos = new PVector(width/2, height);
    this.vel = new PVector(0, -1);
    this.acc = new PVector();
  }

  void applyForce(PVector Force) {
    this.acc.add(Force);
  }

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

  void display() {
    stroke(255);
    strokeWeight(2);
    fill(255, 200);
    rect(this.pos.x, this.pos.y, 50, 10);
  }
}

```

---

<div class="post-metadata">

**Author:** ![josephh](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/josephh/32/210_2.png) [@josephh](https://discourse.processing.org/u/josephh)\
**Post date:** [November 14, 2018, 1:59pm UTC](https://discourse.processing.org/t/im-getting-a-nullpointerexception/5529/2 "2018-11-14T13:59:05Z")

</div>

Hey,

This is because you didn’t initialize your array of `rockets`, you have to say :

```auto
rockets[0] = new Rockets();

```

And this is the right syntax (look at [https://processing.org/reference/class.html](https://processing.org/reference/class.html)) for a class :

```auto
public class Rockets
{
  PVector pos;
  PVector vel;
  PVector acc;

  Rockets() {
    pos = new PVector(width/2, height);
    vel = new PVector(0, -1);
    acc = new PVector();
  }

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

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

  void display() {
    stroke(255);
    strokeWeight(2);
    fill(255, 200);
    rect(pos.x, pos.y, 50, 10);
  }
}

```

---

<div class="post-metadata">

**Author:** ![Str1XHyper](https://avatars.discourse-cdn.com/v4/letter/s/34f0e0/32.png) [@Str1XHyper](https://discourse.processing.org/u/Str1XHyper)\
**Post date:** [November 15, 2018, 7:41am UTC](https://discourse.processing.org/t/im-getting-a-nullpointerexception/5529/3 "2018-11-15T07:41:57Z")

</div>

Hey,

I’m getting a nullpointerexception at `rockets[0] = new Rockets();`

I changed my code to this

```auto
Rockets[] rockets;

public void setup() {
  size(1200, 800);
  rockets[0] = new Rockets();
}

void draw() {
  background(0);
  for (int i=0; i < rockets.length; i++) {
    rockets[i].update();
    rockets[i].display();
  }
}

public class Rockets
{
  PVector pos;
  PVector vel;
  PVector acc;

  Rockets() {
    pos = new PVector(width/2, height);
    vel = new PVector(0, -1);
    acc = new PVector(0, -1);
  }

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

  void display() {
    stroke(255);
    strokeWeight(2);
    fill(255, 200);
    rect(this.pos.x, this.pos.y, 50, 10);
  }
}

```

---

<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:** [November 15, 2018, 8:59am UTC](https://discourse.processing.org/t/im-getting-a-nullpointerexception/5529/4 "2018-11-15T08:59:41Z")

</div>

> [@Str1XHyper](#):
>
> I’m getting a NullPointerException at `rockets[0] = new Rockets();`

Sketches w/ array of objects: 🙂

1. [sketchpad](http://Studio.ProcessingTogether.com/sp/pad/export/ro.9FqnMDLUZqQCV)
2. [sketchpad](http://Studio.ProcessingTogether.com/sp/pad/export/ro.9G5yFfYFFDXi1)
3. [sketchpad](http://Studio.ProcessingTogether.com/sp/pad/export/ro.9qPrLGrYGkr2o)
4. [sketchpad](http://Studio.ProcessingTogether.com/sp/pad/export/ro.9lMOG-RZX8JDk)
5. [sketchpad](http://Studio.ProcessingTogether.com/sp/pad/export/ro.9WgKFjA6WFscW)
6. [sketchpad](http://Studio.ProcessingTogether.com/sp/pad/export/ro.9Ix4oqMF4OK6g)

- [Why do I get a NullPointerException? - Processing 2.x and 3.x Forum](http://Forum.Processing.org/two/discussion/8071/why-do-i-get-a-nullpointerexception)
