# Loops with different colors

why doesn’t this code work?

``````	color[] c = new color ;

int x;
int l;

void setup(){
size(900, 550);

c=color(8,100, 0);
c=color(12);
c=color(16);
c=color(4);
c=color(4);
c=color(4);

}

void draw() {
int x = 0;
int l = 125;
for (int y = 0; y <= 6; y= y + 1 ) {
fill(c[x];
rect(width - 35, l, 50, 50);
x++;
l = l + 75;
}
}

``````
1 Like

Hi @Kenga123456 ,

When asking a question on the forum, it’s better to provide more details about what you are trying to solve and what you already did (what doesn’t work…) Quick tip : you can press `Ctrl+T` in the Processing IDE to auto format your code (correct indentation for example)

I your code there’s an error at this line : `fill(c[x];` but it may be caused by copy pasting…

The first error that comes out is :

``````ArrayIndexOutOfBoundsException: 6
``````

when trying to access a color in your array.

If you look at `y` in the for loop, it goes from `0` to what? nvm i solved it

``````color[] c = new color;
int x;

void setup() {
size(900, 550);
c=color(255, 0, 0);
c=color(0, 255, 0);
c=color(255, 255, 0);
c=color(0, 0, 255);
c=color(64, 224, 208);
c=color(255);
}

void draw() {
x = 0;
rectMode(CENTER);

for (int i = 125; i <= 500; i = i+75 ) {
fill(c[x]);
rect(width - 35, i, 50, 50);
x++;

}
}

``````
1 Like