I'm working on an assignment where I make a maze from a text file reader. I have made a method which lets me select a text file and convert it to a maze but I'm having trouble extracting the maze from the method. I'm getting a NullPointerException
on line 28 which is this: X = Maze[0].length;
I'm stuck on why the method isn't returning my array and also how I can have the method return the StartX and StartY position.
package Innlevering09;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import javafx.application.Application;
import javafx.stage.FileChooser;
import javafx.stage.FileChooser.ExtensionFilter;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.GridPane;
public class MazeGame extends Application {
MazeRoute[][] Maze;
int X;
int Y;
int StartX;
int StartY;
Player Player;
public void start(Stage primaryStage) {
try {
GridPane root = new GridPane();
Player Player = new Player(StartX, StartY);
Maze = FileReader();
X = Maze[0].length;
Y = Maze.length;
root.add(Player.getAppearance(), Player.getXpos(), Player.getYpos());
for(int x = 0; x<X; x++){
for(int y = 0; y<Y; y++){
root.add(Maze[x][y].getAppearance(), x, y);
}
}
Scene scene = new Scene(root, X*10, Y*10);
//scene.setOnKeyPressed(new FileListener(this));
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public MazeRoute[][] FileReader() {
String Text = "";
File File;
int Row;
FileChooser FileChooser = new FileChooser();
FileChooser.setTitle("Open a textfile");
FileChooser.getExtensionFilters().add(new ExtensionFilter("Text Files", "*.txt"));
File = FileChooser.showOpenDialog(null);
try (Scanner FileReader = new Scanner(File)){
X = FileReader.nextInt();
Y = FileReader.nextInt();
Text = FileReader.nextLine();
MazeRoute[][] Maze = new MazeRoute[X][Y];
while (FileReader.hasNext()){
Text = FileReader.nextLine();
for (int i = 0 ; i < X ; i++){
for (Row = 0 ; Row < Y ; Row++) {
char Character = Text.charAt(i);
switch (Character){
case '#':
Maze[i][Row] = new Wall(i, Row);
break;
case ' ':
Maze[i][Row] = new NormalTile(i, Row);
break;
case '-':
Maze[i][Row] = new EndTile(i, Row);
break;
case '*':
Maze[i][Row] = new NormalTile(i, Row);
StartX = i;
StartY = Row;
break;
}Row++;
}
}
}
}catch (FileNotFoundException Error) {
System.out.println("Cannot open file");
Error.printStackTrace();
}
return Maze;
}
public static void main(String[] args) {
launch(args);
}
}
edit:
for the people of the future this is the code where I solved the problem:
public class MazeGame extends Application {
MazeRoute[][] maze;
int X;
int Y;
int startX;
int startY;
Player player = new Player();
public void start(Stage primaryStage){
try{
maze = fileReader();
player.setXpos(startX);
player.setYpos(startY);
GridPane root = new GridPane();
Scene scene = new Scene(root, Color.BLACK);
Player player = new Player();
for(int x = 0; x<X; x++){
for(int y = 0; y<Y; y++){
root.add(maze[x][y].getAppearance(), maze[x][y].getTileXpos(), maze[x][y].getTileYpos());
}
}
root.add(player.getAppearance(), player.getXpos(), player.getYpos());
primaryStage.setTitle("MazeGame");
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public MazeRoute[][] fileReader() throws FileNotFoundException {
String text = "";
File file;
FileChooser fileChooser = new FileChooser();
fileChooser.setTitle("Open a text file with a maze");
fileChooser.getExtensionFilters().add(new ExtensionFilter("Text Files", "*.txt"));
file = fileChooser.showOpenDialog(null);
Scanner fileScanner = new Scanner(file);
X = fileScanner.nextInt();
Y = fileScanner.nextInt();
text = fileScanner.nextLine();
MazeRoute[][] methodMaze = new MazeRoute [X][Y];
while (fileScanner.hasNext()) {
for (int row = 0 ; row < Y ; row++){
text = fileScanner.nextLine();
for (int i = 0 ; i < X ; i++) {
char character = text.charAt(i);
switch (character) {
case '#':
methodMaze[i][row] = new Wall(i, row);
break;
case ' ':
methodMaze[i][row] = new NormalTile(i, row);
break;
case '-':
methodMaze[i][row] = new EndTile(i, row);
break;
case '*':
methodMaze[i][row] = new NormalTile(i, row);
startX = i;
startY = row;
break;
}
}
}
}
return methodMaze;
}
public static void main(String[] args) {
launch(args);
}
}
the changes I made are the following:
- I made a new array with the maze blocks inside of the method to avoid the possibility of any scoping issues.
- I swapped the
row
andi
in thefor
loop meaning that the loop now goes through a full line before incrementing down to the next one. - I moved
text = fileScanner.nextLine();
to the inside of thefor
loop so that it would actually scan the next line. - I changed the
for
loops to start counting from 0 instead of 1 (stupid mistake I know).