From 43d58c815b770f579a25604688a404e6cc167a4e Mon Sep 17 00:00:00 2001 From: "Sylvain Rager (regar42)" Date: Mon, 1 Jan 2018 18:32:38 +0100 Subject: [PATCH] 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 --- src/maze/Box.java | 6 +++++- src/maze/Maze.java | 8 ++++++++ src/model/MazeAppModel.java | 11 ++++++++++- 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/maze/Box.java b/src/maze/Box.java index 0c17d17..d96a886 100644 --- a/src/maze/Box.java +++ b/src/maze/Box.java @@ -35,7 +35,11 @@ public abstract class Box public ArrayList generateNeighbours() { this.neighbourList = maze.generateNeighbours(this); - return maze.generateNeighbours(this) ; + return this.neighbourList; + } + + public void resetNeighbourList() { + neighbourList = null; } public boolean isWalkable() { diff --git a/src/maze/Maze.java b/src/maze/Maze.java index 45c54b5..d587ea8 100644 --- a/src/maze/Maze.java +++ b/src/maze/Maze.java @@ -510,4 +510,12 @@ public class Maze return maze; } + public void resetNeighbourLists() { + for (Box[] line: maze) { + for (Box box: line) { + box.resetNeighbourList(); + } + } + } + } diff --git a/src/model/MazeAppModel.java b/src/model/MazeAppModel.java index e2467a2..791ce36 100644 --- a/src/model/MazeAppModel.java +++ b/src/model/MazeAppModel.java @@ -37,7 +37,12 @@ public class MazeAppModel extends Observable { } else { previous = (Previous) Dijkstra.dijkstra(maze, departure); - shortest = previous.getShortestPathTo(arrival); + if (((Box) arrival).getNeighbourList() == null) { + shortest = null; + } + else { + shortest = previous.getShortestPathTo(arrival); + } } setChanged(); notifyObservers(MazeAppModelMessage.MazeRenewal); @@ -98,12 +103,14 @@ public class MazeAppModel extends Observable { public void setWBox(int boxLine, int boxRow) { maze.getMaze()[boxLine][boxRow] = new WBox("W", boxLine, boxRow, maze); + maze.resetNeighbourLists(); initModelFromMaze(); saved = false; } public void setEBox(int boxLine, int boxRow) { maze.getMaze()[boxLine][boxRow] = new EBox("E", boxLine, boxRow, maze); + maze.resetNeighbourLists(); initModelFromMaze(); saved = false; } @@ -116,6 +123,7 @@ public class MazeAppModel extends Observable { new EBox("E", departureLine, departureRow, maze); } maze.getMaze()[boxLine][boxRow] = new DBox("D", boxLine, boxRow, maze); + maze.resetNeighbourLists(); initModelFromMaze(); saved = false; } @@ -128,6 +136,7 @@ public class MazeAppModel extends Observable { new EBox("E", arrivalLine, arrivalRow, maze); } maze.getMaze()[boxLine][boxRow] = new ABox("A", boxLine, boxRow, maze); + maze.resetNeighbourLists(); initModelFromMaze(); saved = false; }