ArrayList function add doesn't work

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:

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!

1 Like

I don’t know

Try

Make it

ArrayList<Ball> ballCollection;

1 Like

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

1 Like

When you move this into setup ()

Not copy but move

Or restart processing…?

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

1 Like

Are you working in java mode?

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

2 Likes

Yes…but it still doesnt work

1 Like

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

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

2 Likes