0

So I'm trying to create a grid of cells that can each as as their own onTouch object. I'm having a lot of issues but the main one is this exception that occurs while trying to populate the columns and rows of cells. Right now I'm trying to implement a Try/Catch method to resolve it but no luck yet. I thought maybe if I subtract from (j), the number of columns, it might stop the last two columns from populating (realistically I knew that wouldn't work).

Anyway, here's the code:

// creates cells
NavCell[][] mCells = new NavCell[mCellCols][mCellRows];
//System.out.println( "Rows " + mCellRows + " and Cols: " + mCellCols);
for (int j = 0; j < 18; j++)
{
    System.out.println("inside rows       " + j);
    if (mCells[j] != null)
    {
        for (int i = 0; i < mCellCols; i++)
        {
            System.out.println("inside columns      " + i);

            mCells[j][i] = new NavCell();
            mCells[j][i].setBounds(
                i * CELL_SIZE,
                j * CELL_SIZE,
                (i * CELL_SIZE) + CELL_SIZE,
                (i * CELL_SIZE) + CELL_SIZE);
            // drawCells(i, j);
            // System.out.println( "Lazer-3 " + CELL_SIZE);
            // System.out.println( "Lazer-4 " + i);

            System.out.println("Hello Dude" + j );
            Canvas canvas = new Canvas();
            drawCells(canvas, j, i);
        }
        try {
            int Z = j - 2;
            j = Z;
            System.out.println("Hello Matt" + j );
        } catch (ArrayIndexOutOfBoundsException e) {
            Log.e("MainActivity", "Reading list of NavCells failed!", e);
        }
    }

    System.out.println("Lazer-2 " + mCells);
}
Robo Mop
  • 3,485
  • 1
  • 10
  • 23
MegaDude
  • 23
  • 7

1 Answers1

0

There is problem, you declared NavCell[][] mCells = new NavCell[mCellCols][mCellRows]; but is not like that.

You need to declare it NavCell[][] mCells = new NavCell[mCellRows][mCellCols]; is fisrt rows and then columns

So the for will be like:

-- here you just go for all the rows --

for (int i = 0; i < mCellRows; i++){



     --here you go for all the columns in each row --

     for(int a = 0; a < mCelCols; a++){

     }
}
Bryan J. Diaz
  • 374
  • 2
  • 9