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;
}
}
fields[0][0]and the bottom rightfields[8][8]. Arrays are zero-based in Java. Also, " I'm getting an error" is insufficient. Would you take your car to be repaired and refuse to tell the mechanic the symptoms? Please post the complete error message and stack trace by editing your question. Format the stack trace as code.