Okay, let me make sure I understand:
You're trying to create a set of buttons corresponding to strings in the array cellNames[]. You want to place these buttons in a square array, but there's no guarantee that the number of strings in cellNames[] is a perfect square. You're running into errors trying to populate a square grid with a non-square number of strings in cellNames[].
Is that correct?
Assuming I'm not misunderstanding you, I suggest the following changes to your code:
Code:
grid=new ImageButtons[cols][rows];
for(int i=0; i<cols; i++){
for(int j=0; j<rows; j++){
int c = i*rows + j; // Note 1
if (c < cellNames.length){ // Note 2
PImage tmp;
tmp=loadImage(cellNames[c]);
println(cellNames[c]);
grid[i][j]=new ImageButtons(i*cellWidth, j*cellHeight, cellWidth, cellHeight, cellNames[c], tmp);
println("success");
}
}
}
Note 1: Doing this will use each element of cellNames[] exactly once, which I think is what you want.
Note 2: This line checks to see if cellNames[] is big enough to contain an element for this grid box (which I think is what you were trying to do when you were checking for a null value before, but that way doesn't work.)