title: Out of bounds exception - 2D array maze
body: <p>I am new here and new to coding in general! It's exciting to learn but tedious as ever. I am writing a program where a char needs to solve a maze. I am no where near finished here, but for some reason I am getting an out of bounds exception on any array greater than 3 in length. I have no idea why. There are test cases and you can see the small mazes print successfully, however, when I try a bigger maze I get an out of bounds exception.</p>
  
  <p>Any advice?</p>
  
  <pre><code>import java.awt.Point;
  
  
  public class Maze 
  {
      final char WALL = 'x';
      final char BOT = 'r';
      final char FREE = ' ';
  
      private String[] maze;
      private Point position;
      private boolean[][] mazeWalls;
  
      public Maze(String[] strings) // constructor to create maze from main as a 2D array
      {
          maze = strings;
          mazeWalls = new boolean[strings[0].length()][strings.length];
              for (int i = 0; i &lt; mazeWalls.length; i++)
                  for (int j = 0; j &lt; mazeWalls[0].length; j++)
                  {
                      if (strings[i].charAt(j) == WALL) // this is where it shows the out of bounds error on m[3] or m[4]
                          mazeWalls[j][i] = true;
  
                      else if (strings[i].charAt(j) == BOT)
                      {
                          position = new Point(i, j);
                      }
                  }
      }
  
      public static void main (String[] args)
      {
          Maze m;
          /*m = new Maze(new String[] {
                  "xxx",
                  "xrx",
                  "xxx"});
          m = new Maze(new String[] {
                  "xxx",
                  "xrx",
                  "x x"});
          m = new Maze(new String[] {
                  "xxx",
                  "xr      ",
                  "xxx",});*/
          m = new Maze(new String[] {
                  "xxxxx",
                  "xr  x",
                  "x   x",
                  "x    ",
                  "x   x",
                  "x   x",
                  "xxxxx"});
          /*m = new Maze(new String[] {
          "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
          "xr  r                 x                     x",
          "x                    x                     x",
          "x   x                x                      ",
          "x   x                x                     x",
          "x   x                x                     x",
          "x   x                x                     x",
          "x   xxxxxxxxxxxxxxxxxxxxxxxxxx             x",
          "x   x                                      x",
          "x   x                                      x",
          "x   x                                      x",
          "x   x                                      x",
          "x   x                                      x",
          "x   x                                      x",
          "x   x                                      x",
          "x   xxxxxxxxxxxxxx  xxxxxxxxxxxxxxxxxxxxxxxx",
          "x   x                                      x",
          "x   x                                      x",
          "x   x                                      x",
          "x                                          x",
          "x                                          x",
          "x                                          x",
          "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"});*/
                  Robot r = new RandomBot();
              //  Robot r = new RightHandBot();
  
                  int c = 0;
                  System.out.println(m);
                  while (!m.isRobotOnEdge())
                  {
                      c++;
                      Point move = r.getMove(m);
                      m.moveTo(move);
                      System.out.println("Step Count: " + c);
                      System.out.println(m);
                      try {
                          Thread.sleep(100);
                      } catch (InterruptedException e) {}
                  }   
      }
  
      public void moveTo(Point p) 
      {
          /*for (int i = 0; i &lt; mazeWalls.length; i++)
              for (int j = 0; j &lt; mazeWalls[0].length; j++)
              {
                  if (maze[i].charAt(j) == BOT)
                      position = p;
              }*/
      }
  
      public Point getPoint()
      {
          return (Point)position.clone();
      }
  
      public boolean wallObject(Point p)
      {
          return wallHere(position.x, position.y);
      }
  
      public boolean wallHere(int x, int y)
      {
          return x &lt; 0 || x &gt;= mazeWalls.length || y &lt; 0 || y &gt;= mazeWalls[0].length || mazeWalls[x][y];
      }
  
      public boolean isRobotOnEdge() 
      {
                  if (position.x == mazeWalls.length - 1 || position.y == mazeWalls[0].length - 1 || position.x == 0 || position.y == 0)
                      return true;
          return false;
      }
  
      public String toString()
      {
          String s = "";
              for (int j = 0; j &lt; mazeWalls.length; j++)
              {
                  for (int i = 0; i &lt; mazeWalls[0].length; i++)
                  {
                      if (i == position.x &amp;&amp; j == position.y)
                          s += BOT;
                      else if (mazeWalls[i][j] == true)
                          s += WALL;
                      else
                          s += FREE;
                  }
               s += "\n";
              }
  
          return s;
      }
  }
  </code></pre>