0

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;
        }
    }
3
  • Actually, the top left would be fields[0][0] and the bottom right fields[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. Commented Dec 30, 2017 at 5:11
  • @JimGarrison that's why i set both NUM_FIELDS_X and Y to 1 at the beginning, so it wouldn't be 0 - 8 Commented Dec 30, 2017 at 5:16
  • That's not how array declaration works. Please revisit the Java tutorial section on arrays. You cannot change the origin index of an array, it is always ZERO. Commented Dec 30, 2017 at 5:21

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

1 Comment

So should I change it to TextField[][] fields = new TextField[9][9]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.