Reputation: 1
I'm want to create an ArrayList that contains an Array and call the function of the objects inside that array.
I'm trying to call the function display() inside of the Array but I'm getting a NPE even though the array contains an object.
this is my code:
class Ball
{
int x;
int y;
int size;
color c;
Ball()
{
x = int (random(width));
y = int (random(height));
size = int (random(100));
c = color(random(255));
}
void display()
{
fill(c);
ellipse(x,y,size,size);
}
}
ArrayList<Ball[]> balls;
void setup()
{
size(500,500);
balls = new ArrayList<Ball[]>();
for( int i = 0; i < 1; i++)
{
balls.add(new Ball[2]);
println(balls);
}
}
void draw()
{
background(255);
for( int i = 0; i < 1; i++)
{
Ball[] b = balls.get(i);
b[i].display();
}
}
does anybody know how to do this?
Upvotes: 0
Views: 86
Reputation: 31339
You have a list of empty Ball
arrays. Add balls after creating the (empty) array:
void setup()
{
size(500,500);
balls = new ArrayList<Ball[]>();
for( int i = 0; i < 1; i++)
{
Ball[] ballsArray = new Ball[2];
ballsArray[0] = new Ball();
ballsArray[1] = new Ball();
balls.add(ballsArray);
println(balls);
}
}
Upvotes: 1