0

I have a very simple question but i can't figure out why I'm having this exception. I'm trying to create a 2-dimensional Array of objects for a sudoku puzzle, but when I'm initializing i'm getting ArrayIndexOutOfBoundsException. Please help, I've read similar questions and it should be working!

Here I'm declaring the grid(2-dimensional array of objects used and constructor):

    public class Sudoku extends javax.swing.JFrame {
        private int lines;
        Cell[][] grid;

    public Sudoku() {
    initComponents();
    grid = new Cell[lines][lines];

So when i'm cliking a button to set the lines(size length) as shown below

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    lines=10;
    makeGrid(lines);
}

I'm getting the exception:

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 0
at Sudoku.makeGrid(Sudoku.java:146)

    public void makeGrid(int size) {

          for(int i=0;i<size;i++)
                for(int j=0;j<size;j++)    {
    146:                grid[i][j] = new Cell();
                }
          }

4 Answers 4

2

You should move your grid initialization into the make grid method since in the constructor the member lines is still not initialized with your desired value (default value of int is 0 so you get an empty array and you try to access it afterwards with bigger unallocated bounds)

public void makeGrid(int size) {
      this.lines = size; // If you do not need lines anywhere else then it is redundant
      grid = new Cell[size][size];
      for(int i=0;i<size;i++)
            for(int j=0;j<size;j++)    {
              grid[i][j] = new Cell();
            }
      }
Sign up to request clarification or add additional context in comments.

3 Comments

In your example, lines has not been initialized either.
@ChristianKullmann actually if you will look again he is initializing it right before the call to makeGrid, BUT this is still should be moved to the makeGrid method
That is actually an edit. So my comment does not make any sense anymore :)
1

The problem is that the default value for an int is 0.

So when you create your Sudoku object, grid = new Cell[lines][lines]; is equivalent to grid = new Cell[0][0];

Either change your makeGrid method or provide a size in your constructor.

public void makeGrid(int size) {
     this.lines = size;
     grid = new Cell[size][size];
     for(int i=0;i<size;i++){
          for(int j=0;j<size;j++){
              grid[i][j] = new Cell();
          }
     }
 }

Comments

0

grid = new Cell[lines][lines]; creates an array of size [0][0] because lines is still 0 when that statement is run.

Whavetever changes you make to lines later on won't affect the array size, which will remain [0][0]...

Simpler example:

int size = 0;
Object[] array = new Object[size];
size = 1;
System.out.println(array.length); //prints 0, not 1

4 Comments

Thank you for the answer! i didn't have this problem with C++. So the solution is to remove the creation of the array from the constructor and add it to the method?
That is one solution.
Can you recomend another one?
lines seems to always be 10, so you could also simply initialise it from the beginning: private final int lines = 10;
0

Initialize lines before creation of array. Now you creating array with 0x0 dimensions, because lines is 0 be default.

lines = 10; // Add this line   
grid = new Cell[lines][lines];

1 Comment

I don't want the lines value fixed from the beggining that's why I'm having an action listener to a button to get the desired size. Thank you for your answer though!

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.