# ArrayIndexOutOfBounds

**URL:** <https://discourse.processing.org/t/arrayindexoutofbounds/22809>\
**Category:** Libraries\
**Created:** [July 24, 2020, 9:32am UTC](https://discourse.processing.org/t/arrayindexoutofbounds/22809 "2020-07-24T09:32:12Z")\
**Posts on this page:** 2\
**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 24, 2020, 9:32am UTC](https://discourse.processing.org/t/arrayindexoutofbounds/22809/1 "2020-07-24T09:32:12Z")

</div>

Hello guys,

I am new to processing and I’ve been recently trying to learn from some youtube tutorials.  
However, I encountered a problem and hope someone could help me to fix it.  
This is the link of the video I watched:[https://www.youtube.com/watch?v=MKMJz8QYmAU](https://www.youtube.com/watch?v=MKMJz8QYmAU)

So it’s a CA (Game of Live) example done by processing, and I followed the steps properly. However, it wasn’t working after those lines:

```
    if( grid[(x+cols-1)%cols] [(y+rows-1)&rows].type ==1 ) count ++;
    if(grid[(x+cols)%cols][(y+rows-1)&rows].type ==1) count ++;
    if(grid[(x+cols+1)%cols][(y+rows-1)&rows].type ==1) count ++;
    if(grid[(x+cols-1)%cols][(y+rows)&rows].type ==1) count ++;
    if(grid[(x+cols+1)%cols][(y+rows)&rows].type ==1) count ++;
    if(grid[(x+cols-1)%cols][(y+rows+1)&rows].type ==1) count ++;
    if(grid[(x+cols)%cols][(y+rows+1)&rows].type ==1) count ++;
    if(grid[(x+cols+1)%cols][(y+rows+1)&rows].type ==1) count ++;

```

And it gave me an error message: ArrayIndexOutOfBoundsException: 100.  
Strangely, it looks fine and works properly in his computer.

Here is my code:

```
import peasy.*;
import toxi.geom.*;

PeasyCam cam;

int cols = 100;
int rows = 100;

CA grid[][] = new CA[cols][rows];

void setup(){
 size(800,600,P3D);
 cam = new PeasyCam(this,100); 

 //Initilise the grids
 for(int i=0; i<cols; i++){
   for(int j=0; j<rows; j++){
   
     Vec3D ptLoc = new Vec3D (i*10, j*10,0);
     grid[i][j] = new CA(ptLoc,i,j);
   
   
   
    }
  }
}

void draw(){

background(0);
stroke(255);
fill(255,20);
//rect(0,0,600,600);

for(int i=0; i<cols; i++){
for(int j=0; j<rows; j++){

 grid[i][j].run(); 
 }
}
 

for(int i=0; i<cols; i++){
    for(int j=0; j<rows; j++){

    grid[i][j].update(); 
  }
}

 
 }

  class CA{

  Vec3D loc; //a point

  int x;
  int y;
  int type =0;

 int futType = 0;

 CA(Vec3D _loc, int _x,int _y){
 loc = _loc;
 x = _x;
 y = _y;

float rnd = random(100);
if(rnd <50){
  type = 1;
}
}

  void run(){

    display();
    evalN();
  }

  void update(){

    type = futType;

  }

  void evalN(){

        int count = 0;
        if( grid[(x+cols-1)%cols] [(y+rows-1)&rows].type ==1 ) count ++;
        if(grid[(x+cols)%cols][(y+rows-1)&rows].type ==1) count ++;
        if(grid[(x+cols+1)%cols][(y+rows-1)&rows].type ==1) count ++;
        if(grid[(x+cols-1)%cols][(y+rows)&rows].type ==1) count ++;
        if(grid[(x+cols+1)%cols][(y+rows)&rows].type ==1) count ++;
        if(grid[(x+cols-1)%cols][(y+rows+1)&rows].type ==1) count ++;
        if(grid[(x+cols)%cols][(y+rows+1)&rows].type ==1) count ++;
        if(grid[(x+cols+1)%cols][(y+rows+1)&rows].type ==1) count ++;
  
  

      if(type ==1 && count<2){
        futType = 0;
      }
  
      if(type ==1 && count <=3 && count >=2){
        futType =1;
      }
  
  
      if(type ==1 && count>3){
        futType =0;
      }
  
      if(type ==0 && count==3){
        futType =1;
      }
  
  
  

}

  void display(){
    if(type == 1){
      stroke(255);
      strokeWeight(2);
      point(loc.x,loc.y);
    }
  }
}

```

Really appreciate it!

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [July 24, 2020, 9:47am UTC](https://discourse.processing.org/t/arrayindexoutofbounds/22809/2 "2020-07-24T09:47:09Z")

</div>

```java
  void evalN() {
    final int xcols = x + cols, yrows = y + rows;
    int count = 0;

    if (grid[(xcols - 1) % cols][(yrows - 1) % rows].type != 0) ++count;
    if (grid[xcols % cols][(yrows - 1) % rows].type != 0) ++count;
    if (grid[(xcols + 1) % cols][(yrows - 1) % rows].type != 0) ++count;
    if (grid[(xcols - 1) % cols][yrows % rows].type != 0) ++count;
    if (grid[(xcols + 1) % cols][yrows % rows].type != 0) ++count;
    if (grid[(xcols - 1) % cols][(yrows + 1) % rows].type != 0) ++count;
    if (grid[xcols % cols][(yrows + 1) % rows].type != 0) ++count;
    if (grid[(xcols + 1) % cols][(yrows + 1) % rows].type != 0) ++count;

    if (type != 0)
      if (count < 2 | count > 3) futType = 0;
      else futType = 1;
    else if (count == 3) futType = 1;
  }

```
