gpt4 book ai didi

java - 尝试从方法返回对象的二维数组时出现 NullPointerException

转载 作者:行者123 更新时间:2023-12-02 11:25:28 26 4
gpt4 key购买 nike

我正在做一项作业,用文本文件阅读器制作一个迷宫。我制作了一种方法,可以让我选择一个文本文件并将其转换为迷宫,但我无法从该方法中提取迷宫。我在第 28 行收到 NullPointerException ,如下所示: X = Maze[0].length;

我一直困惑为什么该方法不返回我的数组,以及如何让该方法返回 StartX 和 StartY 位置。

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);
}
}

编辑:

对于 future 的人们,这是我解决问题的代码:

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);
}
}

我所做的更改如下:

  • 我在方法内部创建了一个包含迷宫 block 的新数组,以避免出现任何范围问题的可能性。
  • 我交换了 for 循环中的 rowi,这意味着循环现在会遍历整行,然后再向下递增到下一行一个。
  • 我将 text = fileScanner.nextLine(); 移至 for 循环内部,以便它实际上扫描下一行。
  • 我更改了 for 循环,从 0 而不是 1 开始计数(我知道这是一个愚蠢的错误)。

最佳答案

方法FileReader(其名称应以小写字母开头)返回Maze
然而,这是类级别的属性,因为在 try-catch 中声明的同名局部变量超出了范围。
该类级属性尚未分配,因此等于 null

关于java - 尝试从方法返回对象的二维数组时出现 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49672945/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com