corgifan2
corgifan2

Reputation: 3

Java Index out of bounds exception when printing a grid

I'm trying to write a program that prints out domino objects from an array of them into a grid, however whenever I run it I get an index out of bounds exception. I can make a singular line that moves along and draws each domino in the array which works fine, but a grid causes the following:

java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.ArrayList.rangeCheck(ArrayList.java:653)
at java.util.ArrayList.get(ArrayList.java:429)
at DominoGame.drawTable(DominoGame.java:182)
at DominoGame.redraw(DominoGame.java:258)
at DominoGame.restart(DominoGame.java:119)
at DominoGame.<init>(DominoGame.java:91)
at DominoGame.main(DominoGame.java:262)

Here is my code:

    public void drawTable(){
        int i = 0;
        if(this.table.isEmpty()){
        }
        else{
            for(int col = 0;col<this.table.size();col++){
                for(int row = 0;row<this.table.size();row++){
                    this.table.get(i).draw(TABLE_LEFT+DOMINO_SPACING*row, TABLE_TOP+DOMINO_HEIGHT*col);
                    if(this.table.get(i+1) != null){
                        i++;
                    }
                }
            }
        }

    }

Many thanks for any help

EDIT: People have pointed out I had incorrect incrementation in my inner loop. I have fixed this, but I am still getting the error. Thanks

Upvotes: 0

Views: 444

Answers (5)

Wulf
Wulf

Reputation: 712

this.table.get(i+1) //if i+1 does not exist and you want to call it, you get error

Java Api - Methode get() in ArrayList

In the description you will see, in which situation the error will be thrown

Upvotes: 0

corgifan2
corgifan2

Reputation: 3

thanks for you help. Since my code needed a 7 wide grid, I was able to put this code together:

public void drawTable(){
    if(this.table.isEmpty()){
    }
    else{
        int rowCount = 0;
        int colCount = 0;
        for(int i = 0;i<this.table.size();i++){
            if(i%7 == 0){
                rowCount++;
                colCount = 0;
            }
            this.table.get(i).draw(TABLE_LEFT+DOMINO_SPACING*colCount, TABLE_TOP+DOMINO_HEIGHT*rowCount); 
            colCount++;
        }
    }
}

This code accomplished what I want, printing 7 dominoes in one row, then moving to the next and printing another 7. This could be made to work for any grid size just with changing the 7 to another number.

I'd just like to thanks everyone again for your help, its made this process a bit less face-desk worthy than it otherwise would have been.

Upvotes: 0

Quang Vien
Quang Vien

Reputation: 318

You can modify your code:

if(this.table.get(i+1) != null){
   i++;
}

to

if (i < this.table.size()-1 && this.table.get(i+1) != null) {
   i++;
}

When i = this.table.size() - 1, this.table.get(i+1) will raise IndexOutOfBoundsException.

Upvotes: 0

Lars Gendner
Lars Gendner

Reputation: 1982

You increment col instead of row in your inner loop. Furthermore, you iterate over your table in a strange way: The outer loop iterates once over all elements with col, the inner loop does the same with row (with above mentioned typo), and additionally you have i, which will definitely grow bigger than table's size, and is used to get elements from table.

Upvotes: 0

mangusta
mangusta

Reputation: 3544

Assuming that your variable "i" is supposed to be incremented at most col*row times, probably its value eventually exceeds the size of table.

Upvotes: 3

Related Questions