# ArrayList function add doesn't work

**URL:** https://discourse.processing.org/t/arraylist-function-add-doesnt-work/22359
**Category:** Coding Questions
**Tags:** homework
**Created:** [July 3, 2020, 3:24pm UTC](https://discourse.processing.org/t/arraylist-function-add-doesnt-work/22359 "2020-07-03T15:24:58Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![Kai](https://avatars.discourse-cdn.com/v4/letter/k/cc9497/32.png) [@Kai](https://discourse.processing.org/u/Kai)
#### Post date: [July 3, 2020, 3:24pm UTC](https://discourse.processing.org/t/arraylist-function-add-doesnt-work/22359/1 "2020-07-03T15:24:58Z")

</div>

Hello guys,

I’m new to processing and have been recently following some youtube online tutorials to learn.  
The topic is called ArrayList. However, when I followed the scripts some errors occurred, suggesting"The function “add(Ball) does not exist”. It works in the tutorial but not in my case and I don’t know why.

Here is the script:

```auto
ArrayList ballCollection;

void setup(){
  size(600,600);
  smooth();
  ballCollection = new ArrayList(); 
}

void draw(){
  background(0);
  Ball myBall = new Ball(random(0,200),random(0,200));
  ballCollection.add(myBall);
 
  for(int i=0;i<ballCollection.size();i++)
  {
    Ball myB = (Ball) ballCollection.get(i);
    myB.run();
  }
}

class Ball{

  //global variables
  float x = 0;
  float y = 0;
  float speedX = 4;
  float speedY = 0.5;

  //constructor
  Ball(float _x, float _y)
  {
      x = _x;
      y = _y;
  }

  //fuctions
  void display(){
    ellipse(x,y,20,20);
  }

  void move(){
    x +=speedX;
    y +=speedY;
  }

  void bounce(){
    if(x>width){
      speedX *=-1;
    }
    
    if(x<0){
      speedX *=-1;
    }
    
    if(y>width){
      speedY *=-1;
    }
    
    if(y<0){
      speedY *=-1;
    }
  }
  
  void gravity(){
    speedY+=0.2;
  }

  void run(){
    display();
    move();
    bounce();
    gravity();
  }

}

```

Thank you, guys!

---

<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: [July 3, 2020, 3:33pm UTC](https://discourse.processing.org/t/arraylist-function-add-doesnt-work/22359/2 "2020-07-03T15:33:47Z")

</div>

I don’t know

Try

> [@Kai](#):
>
> ArrayList ballCollection;

Make it

`ArrayList<Ball> ballCollection;`

---

<div class="post-metadata">

### Author: ![Kai](https://avatars.discourse-cdn.com/v4/letter/k/cc9497/32.png) [@Kai](https://discourse.processing.org/u/Kai)
#### Post date: [July 3, 2020, 3:45pm UTC](https://discourse.processing.org/t/arraylist-function-add-doesnt-work/22359/3 "2020-07-03T15:45:21Z")

</div>

Hello,  
I’ve tried that, unfortunately it doesn’t work:(

---

<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: [July 3, 2020, 3:56pm UTC](https://discourse.processing.org/t/arraylist-function-add-doesnt-work/22359/4 "2020-07-03T15:56:34Z")

</div>

> [@Kai](#):
>
> Ball myBall = new Ball(random(0,200),random(0,200));  
> ballCollection.add(myBall);

When you move this into setup ()

Not copy but move

---

<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: [July 3, 2020, 4:02pm UTC](https://discourse.processing.org/t/arraylist-function-add-doesnt-work/22359/5 "2020-07-03T16:02:14Z")

</div>

Or restart processing…?

---

<div class="post-metadata">

### Author: ![Kai](https://avatars.discourse-cdn.com/v4/letter/k/cc9497/32.png) [@Kai](https://discourse.processing.org/u/Kai)
#### Post date: [July 3, 2020, 4:06pm UTC](https://discourse.processing.org/t/arraylist-function-add-doesnt-work/22359/6 "2020-07-03T16:06:30Z")

</div>

It syas: "The import java.util.ArrayList conflicts with a type defined in the same file  
"

---

<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: [July 3, 2020, 4:33pm UTC](https://discourse.processing.org/t/arraylist-function-add-doesnt-work/22359/7 "2020-07-03T16:33:05Z")

</div>

Are you working in java mode?

---

<div class="post-metadata">

### Author: ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)
#### Post date: [July 3, 2020, 5:00pm UTC](https://discourse.processing.org/t/arraylist-function-add-doesnt-work/22359/8 "2020-07-03T17:00:06Z")

</div>

The code works fine in Java mode 😄 (at least on my machine)

---

<div class="post-metadata">

### Author: ![Kai](https://avatars.discourse-cdn.com/v4/letter/k/cc9497/32.png) [@Kai](https://discourse.processing.org/u/Kai)
#### Post date: [July 4, 2020, 7:27am UTC](https://discourse.processing.org/t/arraylist-function-add-doesnt-work/22359/9 "2020-07-04T07:27:25Z")

</div>

Yes…but it still doesnt work

---

<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: [July 4, 2020, 11:04am UTC](https://discourse.processing.org/t/arraylist-function-add-doesnt-work/22359/10 "2020-07-04T11:04:32Z")

</div>

I am sorry to hear this

Did you try to restart your computer?

Are you working in processing IDE?

Can you try re-install it?

Chrisir

---

<div class="post-metadata">

### Author: ![Kai](https://avatars.discourse-cdn.com/v4/letter/k/cc9497/32.png) [@Kai](https://discourse.processing.org/u/Kai)
#### Post date: [July 5, 2020, 5:08am UTC](https://discourse.processing.org/t/arraylist-function-add-doesnt-work/22359/11 "2020-07-05T05:08:52Z")

</div>

No worries! the problem was fixed, I shouldn’t name the file as “ArrayList”, this might confuse the system lol
