So I wrote this code for generating a maze, when I run it i get ArratOutOfBoundException. sometimes it gives me the error after generating the maze once but sometimes I generate couple time then it crashes. I feel like my error is in the while loop but can't find it.

import java.util.Random;


//0 = north 1= east , 2 = south, 3 = west
// The StudentMazeGenerator class is where you should implement your maze
// generation algorithm.  Presently, it returns a newly-created maze, in which
// every wall exists.

public class StudentMazeGenerator implements MazeGenerator
{
    private int height = 0;
    private int width = 0;
    private boolean[][] currMazeLoc;
    //private boolean visited;
    private int randomNumInt = 4;
    int[] listArray = {0,1,2,3};
    Random random = new Random();


    public Maze generateMaze(int width, int height, MazeGeneratorListener listener)
    {
        this.width = width;
        this.height = height;

        Maze maze = MazeFactory.createMaze(width, height);
        listener.startingMazeGeneration(maze);
        currMazeLoc = new boolean[width][height];
        for(int i = 0; i<width; i++)
            for(int j =0; j <height; j++)
                currMazeLoc[i][j] = false;
        generatePath(maze, 0, 0, listener);
        return maze;

    }

    public void generatePath(Maze maze, int w, int h,MazeGeneratorListener  listener){
        //  shuffle();

        currMazeLoc[w][h] = true;

        while( !currMazeLoc[w+1][h] || !currMazeLoc[w][h+1] || !currMazeLoc[w-1][h] || !currMazeLoc[w][h-1])
        {

            Random random = new Random();
            int temp = random.nextInt(randomNumInt);
            if(temp == 0) //North wall/cell
            {
                if(w < width-1)  
                    if(!currMazeLoc[w+1][h])  
                    {  
                        maze.removeWallNorthOf(w, h); 
                        listener.mazeModified(maze);
                        w++;  
                        generateMaze(maze, listener); 
                        if(w < width-1)
                            generatePath(maze, w, h, listener);

                    }  

            }  
            if(temp == 1)  //east wall/cell
            {  
                if(h < height-1)  
                    if(!currMazeLoc[w][h+1])  
                    {  
                        maze.removeWallEastOf(w, h); 
                        listener.mazeModified(maze);
                        h++;  
                        generateMaze(maze, listener); 
                        if(h < 0)
                            generatePath(maze, w, h, listener);
                    }  
            }  
            if(temp == 2)  //south wall/cell
            {  
                if(w > 0)  
                    if(!currMazeLoc[w-1][h])  
                    {  
                        maze.removeWallSouthOf(w,h);
                        listener.mazeModified(maze);
                        w--;  
                        generateMaze(maze, listener);  
                        if(w > 0)
                            generatePath(maze, w, h, listener);
                    }  


            }  
            if(temp == 3)  //west wall/cell
            {  

                if(h > 0)

                    if(!(currMazeLoc[w][h-1])) 
                    {

                        ///.removeWallNorthOf(x,y)
                        //y--;
                        maze.removeWallWestOf(w, h); 
                        listener.mazeModified(maze);
                        h--;  
                        generateMaze(maze, listener);
                        if(h > 0)
                            generatePath(maze, w, h, listener);

                    }        
                return;     
            }
        }

    }
    //intArray.length-1;
    //int temp = listArray[i]
    //create an array of the (0 to 3) for N, S , E, W
    //create a method that shuffles the array N, E, S, W
    //  public void shuffle()
    //  {
    //      int[] listArray = new int[] {0,1,2,3};
    //      for (int i=0; i<listArray.length; i++)
    //      {
    //          int randomNum = random.nextInt(listArray.length -1);
    //          int temp = listArray[i];
    //      }
    //  }

    private void generateMaze(Maze maze2, MazeGeneratorListener listener) {
        // TODO Auto-generated method stub

    }

    public boolean unvisitedCell(int x, int y) {
        if (( x == 0) && (y == 0))
            return (currMazeLoc[x+1][y] && currMazeLoc[x][y+1]);
        else if((x == (width - 1)) && (y == (height - 1)))  
            return (currMazeLoc[x-1][y] && currMazeLoc[x][y-1]); 
        else if((x == 0) && (y == (height - 1)))
            return (currMazeLoc[x][y-1] && currMazeLoc[x+1][y]); 
        else if (x == (width -1) && (y == 0))
            return (currMazeLoc[x -1][y] && currMazeLoc[x] [y +1]);
        else
            return (currMazeLoc[x+1][y] && currMazeLoc[x][y+1] && currMazeLoc[x-1][y] && currMazeLoc[x][y-1]); 
        //return false;
    }                  
}

check the error message. it clearly states at what line the error is being thrown, and the (non existing) index you're trying to access.

An ArrayOutOfBoundsException is thrown when you have an array with n elements, and try to get the element with element n (or higher), or an index lower then 0.

for an array with n elements, the possible indexes vary from 0 to (n-1)

thats the exceptopn i get ..line 46 is line 41 in this code i pasted here and 61 is 56in here

java.lang.ArrayIndexOutOfBoundsException: 10
    at StudentMazeGenerator.generatePath(StudentMazeGenerator.java:46)
    at StudentMazeGenerator.generatePath(StudentMazeGenerator.java:61)
    at StudentMazeGenerator.generatePath(StudentMazeGenerator.java:109)
    at StudentMazeGenerator.generatePath(StudentMazeGenerator.java:61)
    at StudentMazeGenerator.generatePath(StudentMazeGenerator.java:109)
    at StudentMazeGenerator.generatePath(StudentMazeGenerator.java:61)
    at StudentMazeGenerator.generatePath(StudentMazeGenerator.java:61)
    at StudentMazeGenerator.generatePath(StudentMazeGenerator.java:61)
    at StudentMazeGenerator.generatePath(StudentMazeGenerator.java:61)
    at StudentMazeGenerator.generatePath(StudentMazeGenerator.java:61)
    at StudentMazeGenerator.generatePath(StudentMazeGenerator.java:89)
    at StudentMazeGenerator.generatePath(StudentMazeGenerator.java:89)
    at StudentMazeGenerator.generatePath(StudentMazeGenerator.java:61)
    at StudentMazeGenerator.generatePath(StudentMazeGenerator.java:61)
    at StudentMazeGenerator.generatePath(StudentMazeGenerator.java:61)
    at StudentMazeGenerator.generateMaze(StudentMazeGenerator.java:36)
    at MazeFrame$MazeGenerationThread.run(MazeFrame.java:275)

well, you're trying to read the tenth element of an array which contains maximum 9 elements, there's the problem

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.