Help! ArrayIndexOutOfBoundsException
Hello, I have to make a sudoku-like GUI puzzle for a program. I was originally going to do this by using a GridLayout but decided it was easier toedit the JTextFields by making a 2D array of JTextFields. However, I am getting an ArrayIndexOutOfBoundsException and I have no idea why. Can someone please help?
This is the section of code that is getting the error:
private Component getPuzzle() {
JPanel puzzlePanel = new JPanel();
int col = 16;
int row = 16;
JTextField sudoku[][] = new JTextField[col][row];
//puzzlePanel.setLayout(new GridLayout(col,row,1,1));
puzzlePanel.add(sudoku[col][row]);
for (int i =0; i < row*col; i++) {
puzzlePanel.add(new JTextField());
}
return puzzlePanel;
}
Re: Help! ArrayIndexOutOfBoundsException
What is your actual error? What line is it on?
Hint: When do you initialize your the values contained by your array?
Re: Help! ArrayIndexOutOfBoundsException
Also if anyone could suggest a way to compare values in the rows and columns in the array. Is it possible to use the compareTo() method to compare the Strings in the text fields? For example compare all the values in [1][0],[2][0],[0][1],[0][2] ect to make sure 2 of the same values aren't used twice?
Re: Help! ArrayIndexOutOfBoundsException
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 16
at sudoku.SudokuBoard.getPuzzle(SudokuBoard.java:84)
line 84 is: puzzlePanel.add(sudoku[col][row]);
Re: Help! ArrayIndexOutOfBoundsException
Re: Help! ArrayIndexOutOfBoundsException
I changed JTextField sudoku[][] = new JTextField[col][row]; to JTextField sudoku[][] = new JTextField[20][20]; and that got rid of the out of bounds exception but now there is a null pointer exception
Exception in thread "main" java.lang.NullPointerException
at java.awt.Container.addImpl(Unknown Source)
at java.awt.Container.add(Unknown Source)
at sudoku.SudokuBoard.getPuzzle(SudokuBoard.java:83)
I'm thinking about just going back to the GridLayout because I had that atleast displaying what I needed but is there a way to compare JTextFields in a GridLayout and add a 2D String array to the layout?
Re: Help! ArrayIndexOutOfBoundsException
There is a variable on line 83 that has a null value. Look at that line, find the variable and backtrack through your code to find out why and change it to give the variable a valid value.
Quote:
is there a way to compare JTextFields
How do you mean compare? Do you mean the contents of the text fields?
In what kind of a component do you want to display the 2D String array?
Re: Help! ArrayIndexOutOfBoundsException
Quote:
Originally Posted by
Norm
How do you mean compare? Do you mean the contents of the text fields?
In what kind of a component do you want to display the 2D String array?
I'm creating a hexadecimal sudoku puzzle so I need to compare the strings 0-9 and a-f to make sure there are no repeats. And I was going to display the 2D String array in a grid of JTextFields each holding 1 value
Re: Help! ArrayIndexOutOfBoundsException
Then you mean you want to compare the contents of two text fields? Is that right?
Re: Help! ArrayIndexOutOfBoundsException
Well it is a 16 x 16 grid so I would need to compare 16 text fields
Re: Help! ArrayIndexOutOfBoundsException
What is it that you want to detect by comparing the text fields?
For example: Do any two textfields have the same value?
If all the textfields are in an array, then going thru the array should be easy.
Quote:
16 x 16 grid so I would need to compare 16 text fields
Please explain this. 16x16 = 256