Can't figure out my OutOfBoundsException
<p>The purpose of this was to brute force the Knights Tour by having it figure out which moves were legal before choosing one of the ones that are legal. I am new to Java but I feel like my error is in my inability to comprehend how to handle this problem:</p>

<pre><code>import java.util.*;



public class KnightTour
{
    public static void main( String[] args )
    {
    KnightTour kt = new KnightTour(8, 8);
    int tries = 3;
    int tryCount = 0;

    while(tryCount &lt; tries )
    {
        kt.makeMoves();
    }
}

int rows = 0;  //change to args later
int columns = 0;  //change to args later
int tries = 0; //change to args later
String[][] saves;   

int tryCount = 0;
int turnNr = 2;
int wait = 0;

Random rand = new Random();

int xCurrent = 1;
int yCurrent = 1;

int[] xMoves = { 1, 2, -1, -2, 1, 2, -1, -2 };
int[] yMoves = { 2, 1, 2, 1, -2, -1, -2, -1 };

public KnightTour( int x, int y)
{
    rows = x;
    columns = y;
    saves = new String[y][x];

    for (int i = 0; i &lt; y; i++) 
    {
        for (int j = 0; j &lt; x; j++) 
        {
            saves[i][j] = Integer.toString(0);
        }
    }
    saves[0][0] = Integer.toString(1);
}

private void makeMoves()
{
    int k = 0;

    while( k &lt; (rows * columns ) )
    {
        int[] d = { 0, 0, 0, 0, 0, 0, 0, 0 }; // holds move legality
        int i = 0;

        while( i &lt; d.length ) // loop determines which moves are legal
        {
            if( xCurrent + xMoves[ i ] &gt; 0 &amp;&amp; xCurrent + xMoves[ i ] &lt; rows )
            {
                if( xCurrent + yMoves[ i ] &gt; 0 &amp;&amp; yCurrent + yMoves[ i ] &lt; rows )
                    d[ i ] = 1;
            }
            i++;
        }

        int t = 0;
        int w = 0;

        while( t &lt; d.length ) // checks if no moves are legal
        {   
            if( d[ t ] == 0 )
            {
                w++;
            }
            t++;
        }

        if( w == 8 )
        {
            writeFailures(); // fills the rest of the grid with "x"'s
            k = (rows * columns);  // breaks the loop
        }
        else
        {
            w = 0;
            chooseMove( d );
        }
        k++;
    }
    printSolution();
}

private void chooseMove(int[] d) // chooses a move that was previously determined to be legal randomly and checks if it is available
{
    System.out.println( "trace" );
    Random rand = new Random();
    int r = rand.nextInt(8);
    switch( r )
    {
    case 0:
        if( d[ 0 ] == 1 )
        {
            setX( xCurrent + xMoves[ 0 ] );
            setY( yCurrent + yMoves[ 0 ] );
            saves[yCurrent][xCurrent] = Integer.toString(turnNr);
            turnNr++;
        }
        else
        {
            chooseMove(d);
        }
        break;

    case 1:
        if( d[ 1 ] == 1 )
        {
            setX( xCurrent + xMoves[ 1 ] );
            setY( yCurrent + yMoves[ 1 ] );
            saves[yCurrent][xCurrent] = Integer.toString(turnNr);
            turnNr++;
        }
        else
        {
            chooseMove(d);
        }
        break;

    case 2:
        if( d[ 2 ] == 1 )
        {
            setX( xCurrent +  xMoves[ 2 ] );
            setY( yCurrent + yMoves[ 2 ] );
            saves[yCurrent][xCurrent] = Integer.toString(turnNr);
            turnNr++;
        }
        else
        {
            chooseMove(d);
        }
        break;

    case 3:
        if( d[ 3 ] == 1 )
        {
            setX( xCurrent + xMoves[ 3 ] );
            setY( yCurrent + yMoves[ 3 ] );
            saves[yCurrent][xCurrent] = Integer.toString(turnNr);
            turnNr++;
        }
        else
        {
            chooseMove(d);
        }
        break;

    case 4:
        if( d[ 4 ] == 1 )
        {
            setX( xCurrent + xMoves[ 4 ] );
            setY( yCurrent + yMoves[ 4 ] );
            saves[yCurrent][xCurrent] = Integer.toString(turnNr); // LINE 166
            turnNr++;
        }
        else
        {
            chooseMove(d);
        }
        break;

    case 5:
        if( d[ 5 ] == 1 )
        {
            setX( xCurrent + xMoves[ 5 ] );
            setY( yCurrent + yMoves[ 5 ] );
            saves[yCurrent][xCurrent] = Integer.toString(turnNr);
            turnNr++;
        }
        else
        {
            chooseMove(d);
        }
        break;

    case 6:
        if( d[ 6 ] == 1 )
        {
            setX( xCurrent + xMoves[ 6 ] );
            setY( yCurrent + yMoves[ 6 ] );
            saves[yCurrent][xCurrent] = Integer.toString(turnNr);
            turnNr++;
        }
        else
        {
            chooseMove(d);
        }
        break;

    case 7:
        if( d[ 7 ] == 1 )
        {
            setX( xCurrent + xMoves[ 7] );
            setY( yCurrent + yMoves[ 7 ] );
            saves[yCurrent][xCurrent] = Integer.toString(turnNr);
            turnNr++;
        }
        else
        {
            chooseMove(d);
        }
        break;
    default:
        System.out.println( "error" );
    }
}

public int getX() 
{
    return xCurrent;
}

public void setX(int x) 
{
    xCurrent = x;
}

public int getY() 
{
    return yCurrent;
}

public void setY(int y) 
{
     yCurrent = y;
}

private void writeFailures() // writes an "x" to empty spots in the save array when no legal moves are found
{
    for (int i = 0; i &lt; saves.length; i++) 
    {
        for (int j = 0; j &lt; saves[i].length; j++) 
        {
            if( saves[i][j] == "0");
                saves[i][j] = "x";
        }            
    }
}

private void printSolution()
{
    for (int i = 0; i &lt; saves.length; i++) 
    {
        for (int j = 0; j &lt; saves[i].length; j++) 
        {
            System.out.print(saves[i][j] + " ");
        }
        System.out.println("");
    }
    System.out.println("");
}
}
</code></pre>

<p>The error I get is:</p>

<pre><code>Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -2
at KnightTour.chooseMove(KnightTour.java:166)
at KnightTour.makeMoves(KnightTour.java:91)
at KnightTour.main(KnightTour.java:14)
</code></pre>

<p><strong>EDIT:</strong> Now the code line numbers are OK.</p>
