Fix null pointer exception when there is no path
maze: * Add methods to Box and Maze to reset the neighbor lists of boxes model: * If the neighbor list of the arrival box is null, it means that no path exist between departure and arrival, and we therefore display the pop-up which is already displayed when the user tries to draw the path on a maze where there is no departure and/or arrival
This commit is contained in:
1 parent
024a4f70c3
commit
43d58c815b
3 files changed
+22
-1
No files matched your search
+5
-1
@@ -35,7 +35,11 @@ public abstract class Box
|
|||||||
|
|
||||||
public ArrayList<VertexInterface> generateNeighbours() {
|
public ArrayList<VertexInterface> generateNeighbours() {
|
||||||
this.neighbourList = maze.generateNeighbours(this);
|
this.neighbourList = maze.generateNeighbours(this);
|
||||||
return maze.generateNeighbours(this) ;
|
return this.neighbourList;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void resetNeighbourList() {
|
||||||
|
neighbourList = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isWalkable() {
|
public boolean isWalkable() {
|
||||||
|
|||||||
@@ -510,4 +510,12 @@ public class Maze
|
|||||||
return maze;
|
return maze;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void resetNeighbourLists() {
|
||||||
|
for (Box[] line: maze) {
|
||||||
|
for (Box box: line) {
|
||||||
|
box.resetNeighbourList();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -37,8 +37,13 @@ public class MazeAppModel extends Observable {
|
|||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
previous = (Previous) Dijkstra.dijkstra(maze, departure);
|
previous = (Previous) Dijkstra.dijkstra(maze, departure);
|
||||||
|
if (((Box) arrival).getNeighbourList() == null) {
|
||||||
|
shortest = null;
|
||||||
|
}
|
||||||
|
else {
|
||||||
shortest = previous.getShortestPathTo(arrival);
|
shortest = previous.getShortestPathTo(arrival);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
setChanged();
|
setChanged();
|
||||||
notifyObservers(MazeAppModelMessage.MazeRenewal);
|
notifyObservers(MazeAppModelMessage.MazeRenewal);
|
||||||
}
|
}
|
||||||
@@ -98,12 +103,14 @@ public class MazeAppModel extends Observable {
|
|||||||
|
|
||||||
public void setWBox(int boxLine, int boxRow) {
|
public void setWBox(int boxLine, int boxRow) {
|
||||||
maze.getMaze()[boxLine][boxRow] = new WBox("W", boxLine, boxRow, maze);
|
maze.getMaze()[boxLine][boxRow] = new WBox("W", boxLine, boxRow, maze);
|
||||||
|
maze.resetNeighbourLists();
|
||||||
initModelFromMaze();
|
initModelFromMaze();
|
||||||
saved = false;
|
saved = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setEBox(int boxLine, int boxRow) {
|
public void setEBox(int boxLine, int boxRow) {
|
||||||
maze.getMaze()[boxLine][boxRow] = new EBox("E", boxLine, boxRow, maze);
|
maze.getMaze()[boxLine][boxRow] = new EBox("E", boxLine, boxRow, maze);
|
||||||
|
maze.resetNeighbourLists();
|
||||||
initModelFromMaze();
|
initModelFromMaze();
|
||||||
saved = false;
|
saved = false;
|
||||||
}
|
}
|
||||||
@@ -116,6 +123,7 @@ public class MazeAppModel extends Observable {
|
|||||||
new EBox("E", departureLine, departureRow, maze);
|
new EBox("E", departureLine, departureRow, maze);
|
||||||
}
|
}
|
||||||
maze.getMaze()[boxLine][boxRow] = new DBox("D", boxLine, boxRow, maze);
|
maze.getMaze()[boxLine][boxRow] = new DBox("D", boxLine, boxRow, maze);
|
||||||
|
maze.resetNeighbourLists();
|
||||||
initModelFromMaze();
|
initModelFromMaze();
|
||||||
saved = false;
|
saved = false;
|
||||||
}
|
}
|
||||||
@@ -128,6 +136,7 @@ public class MazeAppModel extends Observable {
|
|||||||
new EBox("E", arrivalLine, arrivalRow, maze);
|
new EBox("E", arrivalLine, arrivalRow, maze);
|
||||||
}
|
}
|
||||||
maze.getMaze()[boxLine][boxRow] = new ABox("A", boxLine, boxRow, maze);
|
maze.getMaze()[boxLine][boxRow] = new ABox("A", boxLine, boxRow, maze);
|
||||||
|
maze.resetNeighbourLists();
|
||||||
initModelFromMaze();
|
initModelFromMaze();
|
||||||
saved = false;
|
saved = false;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user