What's causing the Array index out of bounds error?

edited December 2016 in Questions about Code

I am a programming beginner and am unsure what may be causing error: ArrayIndexOutOfBoundsException: 2. I think it's either a logic error in the for loop or a syntax error with the array. Would anyone advise me with what is going wrong in the following code and how you might approach fixing an error like this. If I track the values on paper the numbers seem to be as i intended.

For reference I want the program to store p amount of grey colour values (plus black and white 0, 255) in an array. The values stored in the array occur at equal intervals. e.g p value of 5 should store:{ 0, 51, 102, 153, 204, 255 }.

Thanks for any help.

int p = 1;
int interval = 255 / p;
int[] vGreyPalette = new int [(p+1)];

 void setup() {
   size(200, 200);
   noLoop();
 }

 void draw () {

  for (int i = 0; i == p; i++) {    
    vGreyPalette[i] =  (i * interval);
  }
  fill (vGreyPalette[int(random(p))]);
  rect(0, 0, width, height);

 }

void keyPressed() {
  if (key == 'w') {
    p = p + 1;
    redraw();
  }
}

Answers

  • I think you need to increase the size of the array when you increase p!

  • edited December 2016

    An array of size 2 contains two elements, numbered 0 and 1.

    Your for loop looks odd, specifically the middle bit, i==p. Typically this is i < size of the thing you are iterating over.

  • @neilcsmith_net the size of the array is p so it should scale as p increases(?) @koogs I forgot to mention that p increments when keyPressed is triggered! either way you can write i==p or i < (p+1), i think they are the same in this case

  • edited December 2016

    Err, no.

    It loops whilst the middle clause is true, which it never is in your case. Add a println to the body to see.

    Your array won't change size just because you increment p. Line 3 executes once using the value of p at that time. Changing p later won't change the size of the array.

    This should all be done using ArrayList imo., a structure designed from the start to be expandable.

  • Answer ✓

    Every array has a length which is the size of the array so lines 12 - 15 become

    for (int i = 0; i < vGreyPalette.length; i++) {    
      vGreyPalette[i] =  (i * interval);
    }
    fill (vGreyPalette[int(random(vGreyPalette.length))]);
    

    It is now impossible to go beyond the array bounds.

    The array size is fixed when it is created i.e. line 3 and cannot be changed at runtime so line 22 does NOT increase the size of the array. To do that Processing provides the append() method

  • So your problem is that you are increasing p, so the random on line 15 is choosing indexes outside the range of elements you actually have. The for loop is never running.

  • I understand now that the array is not 'remade' every time the sketch is redrawn which is what i had hoped. So i need to use append() if i want to have an evolving sized array (i think)

    thanks @koogs, @quark,

  • @koogs or Processing has IntList which would be more suited to this example.

    @duzbot the other option is to create the array once with a size of 255, and make sure only to increase p in keyPressed if it is less than 255. Any value higher than that wouldn't make sense anyway!

Sign In or Register to comment.