0

When I compiled I got here error java.lang.ArrayIndexOutOfBoundsException: -1. I checked code 3 times and no error found there, also I am not writing after end of array.

Random nahoda = new Random();    int[][] minPol = new int[5][5];
int[][] cisPol = new int[5][5];
for(int i = 0;i<5;i++)
{
for(int j=0;j<5;j++)
{
minPol[i][j] = nahoda.nextInt(2);
cisPol[i][j] = 0;
}
}
for(int i = 0;i<5;i++)
{
    for(int j=0;j<5;j++)
    {
        if(minPol[i][j]!=0)
        {
        if(i != 0 || j != 0 || i != 4 || j != 4)
        {
            cisPol[i+1][j+1]++;
            cisPol[i+1][j-1]++;
            cisPol[i-1][j+1]++;
            cisPol[i-1][j-1]++;
            cisPol[i+1][j]++;
            cisPol[i-1][j]++;
            cisPol[i][j+1]++;
            cisPol[i][j-1]++;
        }
        else
        {
            if(i == 0)
            {
                if(j == 0)
                {
                    cisPol[i+1][j+1]++;
                    cisPol[i][j+1]++;
                    cisPol[i+1][j]++;
                }
                else if(j == 4)
                {
                    cisPol[i+1][j]++;
                    cisPol[i+1][j-1]++;
                    cisPol[i][j-1]++;
                }
                else
                {
                    cisPol[i+1][j+1]++;
                    cisPol[i][j+1]++;
                    cisPol[i+1][j]++;
                    cisPol[i+1][j-1]++;
                    cisPol[i][j-1]++;
                }
            }
            else if(i == 4)
            {
                if(j == 0)
                {
                    cisPol[i-1][j+1]++;
                    cisPol[i-1][j]++;
                    cisPol[i][j+1]++;
                }
                else if(j == 4)
                {
                    cisPol[i-1][j-1]++;
                    cisPol[i-1][j]++;
                    cisPol[i][j-1]++;
                }
                else
                {
                    cisPol[i][j-1]++;
                    cisPol[i][j+1]++;
                    cisPol[i-1][j+1]++;
                    cisPol[i-1][j]++;
                    cisPol[i-1][j-1]++;
                }
            }
        }            
        }
    }
}

I am beginner in Java and thanks for tips

2
  • 2
    At which line of your code do you get this Exception? Commented Nov 12, 2012 at 14:59
  • when i=4 or j=4„ the index goes out of bounds because of i+1 or j+1 indexes respectively. Commented Nov 12, 2012 at 15:02

2 Answers 2

7

Look at this condition:

if(i != 0 || j != 0 || i != 4 || j != 4)

That doesn't do what you want it to. It will always be true, because i can't be simultaneously equal to 0 and 4.

Therefore you'll end up going into here when j is 0:

cisPol[i+1][j-1]++;

Bang.

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

Comments

0

Try this;

Replace this line;

 if(i != 0 || j != 0 || i != 4 || j != 4)

with

 if( i>=1 && j>=1 && j<4 && i<4)

That will make sure you don't reference from your array with a negative index or a value greater than 4.

Comments

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.