Reputation: 31
I'm trying to create a 9x9 grid of textFields that are all in an array. The top left would be called fields[1][1] and the bottom right fields[9][9]. The code I've written so far is
int NUM_FIELDS_X = 1; //x textField
int NUM_FIELDS_Y = 1; // y textField
int x = 21; // x location of textField
int y = 21; // y location of textField
TextField[][] fields = new TextField[NUM_FIELDS_X][NUM_FIELDS_Y]; {
for (NUM_FIELDS_Y =1; NUM_FIELDS_Y <= 9; NUM_FIELDS_Y++) {
for (NUM_FIELDS_X =1; NUM_FIELDS_X <= 9; NUM_FIELDS_X++) {
fields[NUM_FIELDS_X][NUM_FIELDS_Y] = new TextField();
fields[NUM_FIELDS_X][NUM_FIELDS_Y].setColumns(10);
fields[NUM_FIELDS_X][NUM_FIELDS_Y].setBounds(x, y, 32, 32);
frame.getContentPane().add(fields[NUM_FIELDS_X][NUM_FIELDS_Y]);
x=x+32;
}
y = y+32;
}
}
However I'm getting an error as soon as I launch it, and the for loops do not start. The error message im receiving is
java.lang.ArrayIndexOutOfBoundsException: 1
at sudokuSolver.sudokuInterface.initialize(sudokuInterface.java:70)
at sudokuSolver.sudokuInterface.<init>(sudokuInterface.java:44)
at sudokuSolver.sudokuInterface$1.run(sudokuInterface.java:31)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Thanks for the help Akash Jha and coder-croc, I've managed to fix it. I've changed the code to
int NUM_FIELDS_X = 0; //x number of textField
int NUM_FIELDS_Y = 0; // y number of textField
int x = 21; // x location of textField
int y=21; // y location of textField
TextField[][] fields = new TextField[9][9]; {
for (NUM_FIELDS_Y =0; NUM_FIELDS_Y < 9; NUM_FIELDS_Y++) {
x=21;
for (NUM_FIELDS_X =0; NUM_FIELDS_X <= 8; NUM_FIELDS_X++) {
fields[NUM_FIELDS_X][NUM_FIELDS_Y] = new TextField();
fields[NUM_FIELDS_X][NUM_FIELDS_Y].setColumns(10);
fields[NUM_FIELDS_X][NUM_FIELDS_Y].setBounds(x, y, 32, 32);
frame.getContentPane().add(fields[NUM_FIELDS_X][NUM_FIELDS_Y]);
x=x+32;
}
y = y+32;
}
}
Upvotes: 0
Views: 563
Reputation: 23012
The code which has the problem,
NUM_FIELDS_Y = 1; NUM_FIELDS_Y <= 9;
Your array is from fields[0][0]
to fields[8][8]
for length 9 (index 0 to 8) and NUM_FIELDS_Y <= 9
condition will be going to access fields[9][9]
and thus you are getting the ArrayIndexOutOfBoundException
.
Secondly, you are skipping the 0th index as NUM_FIELDS_Y =1
will access fields[1][1]
instead of fields[0][0]
.
Change your loop,
int xLength = 9;
int yLength = 9;
TextField[][] fields = new TextField[xLength][yLength];
for (int x = 0; x < xLength; x++) {
for (int y = 0; y < yLength; y++) {
// your code
}
}
EDIT
Your fields array has length 1
,
TextField[][] fields = new TextField[NUM_FIELDS_X][NUM_FIELDS_Y];
So fields
can only have one 1 element at index [0][0]
and first iteration is trying to access [1][1]
which is incorrect.
Visit this link to understand more about arrays.
Upvotes: 1
Reputation: 62
You are creating an array of your text field as [1][1] and you are trying to put more than one element in your array... Increase the size of your text field array. That will help
Upvotes: 0