Reputation: 169
When i run the code below on my Android device, I get an ArrayOutOfBounds exception saying y = 6. However, this is theoretically not possible because the bounds of the for-loop is y < 6. I've searched for possible explanations and found none. The size of matrix1Numbers is 6 by 6, holding 0-5 for each dimension.
for (z = 0; z < 6; ++z) {
for (y = 0; y < 6; ++y) {
matrix1Numbers[z][y].addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged( CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (matrix1Numbers[z][y].getText().toString().equals("")) {
for (int a = 0; a < z; ++a) {
for (int b = y; b < 6; ++b) {
matrix1Numbers[a][b].setText("");
matrix1Numbers[a][b].setBackgroundColor(Color.WHITE);
}
}
for (int a = z; a < 6; ++a) {
for (int b = 0; b < y; ++b) {
matrix1Numbers[a][b].setText("");
matrix1Numbers[a][b].setBackgroundColor(Color.WHITE);
}
}
for (int a = z; a < 6; ++a) {
for (int b = y; b < 6; ++b) {
matrix1Numbers[a][b].setText("");
matrix1Numbers[a][b].setBackgroundColor(Color.WHITE);
}
}
} else {
if (z > matrix1MaxX) {
matrix1MaxX = z;
}
if (y > matrix1MaxY) {
matrix1MaxY = y;
}
for (int a = 0; a < matrix1MaxX; ++a) {
for (int b = 0; b < matrix1MaxY; ++b) {
matrix1Numbers[a][b].setBackgroundColor(Color.BLUE);
}
}
}
}
@Override
public void afterTextChanged(Editable s) {
}
});
matrix2Numbers[z][y].addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (matrix2Numbers[z][y].getText().toString().equals("")) {
for (int a = 0; a < z; ++a) {
for (int b = y; b < 6; ++b) {
matrix2Numbers[a][b].setText("");
matrix2Numbers[a][b].setBackgroundColor(Color.WHITE);
}
}
for (int a = z; a < 6; ++a) {
for (int b = 0; b < y; ++b) {
matrix2Numbers[a][b].setText("");
matrix2Numbers[a][b].setBackgroundColor(Color.WHITE);
}
}
for (int a = z; a < 6; ++a) {
for (int b = y; b < 6; ++b) {
matrix2Numbers[a][b].setText("");
matrix2Numbers[a][b].setBackgroundColor(Color.WHITE);
}
}
} else {
if (z > matrix2MaxX) {
matrix2MaxX = z;
}
if (y > matrix2MaxY) {
matrix2MaxY = y;
}
for (int a = 0; a < matrix2MaxX; ++a) {
for (int b = 0; b < matrix2MaxY; ++b) {
matrix2Numbers[a][b].setBackgroundColor(Color.BLUE);
}
}
}
}
@Override
public void afterTextChanged(Editable s) {
}
});
}
Upvotes: 2
Views: 260
Reputation: 115
Check if some other parts of code work with your y
and z
because you are declaring them outside the loop. There's maybe some piece of code that's modifying them and you are unaware of the side effect.
Upvotes: 1