0

Hi I am working on this new Java swing pathfinding project using A* and Dijkstra algorithms, I have made my program run fine but I was wanting to add a third value to my Node, before it would have been Node[x][y] but i am trying to make it take a third value which would be the theta or in this case a robot angle, Node[x][y][theta]. I am getting an error when I do this and to be honest I am not sure exactly how to go about even adding this. The program currently prompts you for the robot angle or theta once you plot the Start or End node.

here is my github repo for the project, it is not updated yet so everything should still be functional the project is under FrcVersion. https://github.com/mohammadawwad/Java-Pathfinding/tree/master/Pathfinding/src/FRCVersion

here is where I get my error

map = new Node[cellsWidth][cellsHeight][(int) theta]; 
for (int x = 0; x < cellsWidth; x++) {
  for (int y = 0; y < cellsHeight; y++) {
      map[x][y][(int) theta] = new Node(2, x, y, theta); 
      //sets all nodes to blank
  }
}

This is the error message

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 2 out of bounds for length 2
        at FRCVersion.FrcApp.cleanMap(FrcApp.java:115)
        at FRCVersion.FrcApp.<init>(FrcApp.java:97)
        at FRCVersion.Main.main(Main.java:5)

I would like to thank everyone in advance for any help or advice. I am sorry if this seems like i am doing something supper stupid its the first time I use nodes and am still learning

moawwad
  • 51
  • 8
  • 1
    `map = new Node[cellsWidth][cellsHeight][(int) theta];` - so `theta` is one of the sizes that you defined. That means the indexes go from `[0, ..., theta-1]`. But then here `map[x][y][(int) theta]` you try to access index `theta` which is out of bounds. – maloomeister May 20 '21 at 07:07
  • Does this answer your question? [What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?](https://stackoverflow.com/questions/5554734/what-causes-a-java-lang-arrayindexoutofboundsexception-and-how-do-i-prevent-it) – maloomeister May 20 '21 at 07:07
  • It's not clear it theta is a property of the node or a third dimension, if it's a dimension like x and y you should add a third loop from 0 to theta-1 and use the loop index variable – Rocco May 20 '21 at 13:00

0 Answers0