# NullPointerException with ArrayList of Objects

**URL:** https://discourse.processing.org/t/nullpointerexception-with-arraylist-of-objects/9895
**Category:** Coding Questions
**Created:** [April 2, 2019, 9:28pm UTC](https://discourse.processing.org/t/nullpointerexception-with-arraylist-of-objects/9895 "2019-04-02T21:28:07Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![LordBruce](https://avatars.discourse-cdn.com/v4/letter/l/6a8cbe/32.png) [@LordBruce](https://discourse.processing.org/u/LordBruce)
#### Post date: [April 2, 2019, 9:28pm UTC](https://discourse.processing.org/t/nullpointerexception-with-arraylist-of-objects/9895/1 "2019-04-02T21:28:07Z")

</div>

So I am trying to get a list of objects, that when something is pressed, it will add new objects to the list to be checked. I keep getting NullPointerException and I can’t figure it out. I have tried adding a new item when I press a key and simplified the code down to just the error, but it isn’t working for me. I don’t understand how I can get a NullPointerException when trying to create a new item. Thank you in advance.

```auto
class Dots {
  PVector position = new PVector(random(width), random(height));
  PVector velocity = new PVector(1, 1);
  PVector acceleration = new PVector(1, 1);
  
  void updatePos() {
   velocity.add(acceleration);
   position.add(velocity);
   circle(position.x, position.y, 10);
  }
}

ArrayList<Dots> dotsList;

void setup() {
  size(640, 640);
}

void draw() {
  background(0);
  //for (int i = 0; i < 1; i++) {
  // circle(dotsList.get(i).position.x, dotsList.get(i).position.y, 10);
  //}
  if (keyCode == CONTROL) {
    dotsList.add(new Dots());  
  }
}

```

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [April 2, 2019, 10:04pm UTC](https://discourse.processing.org/t/nullpointerexception-with-arraylist-of-objects/9895/2 "2019-04-02T22:04:49Z")

</div>

Welcome to the forum!

This ArrayList dotsList; should  
be ArrayList dotsList=new ArrayList ();  
or  
`ArrayList<Dots> dotsList=new ArrayList ();`

Telling the arraylist its type.

Maybe in setup you need to add one dot

Your for loop is not okay

Convention: your class could be named singular, since it’s only one dot. Starting with a capital letter is correct

Regards, Chrisir
