This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12个答案)
4年前关闭。
我正在尝试用Java创建2D游戏,其中提示用户输入行宽和列长。创建一个“世界”对象,该对象在打印时如下所示(其中P是玩家的角色):
然后,用户可以输入向上,向下,向左,向右或退出,这将使P左右移动,如果移动会使角色离开地图,则什么也不做。我的问题是输入宽度和长度后,出现以下错误:
我相信这意味着Java将worldDimensions.length视为空/空?我以为我在创建世界对象时正在为它分配一个值。
这是我的代码:
(12个答案)
4年前关闭。
我正在尝试用Java创建2D游戏,其中提示用户输入行宽和列长。创建一个“世界”对象,该对象在打印时如下所示(其中P是玩家的角色):
P---
----
----
----
然后,用户可以输入向上,向下,向左,向右或退出,这将使P左右移动,如果移动会使角色离开地图,则什么也不做。我的问题是输入宽度和长度后,出现以下错误:
java.lang.NullPointerException
at World.displayWorld(Driver.java:90)
at Driver.main(Driver.java:28)
我相信这意味着Java将worldDimensions.length视为空/空?我以为我在创建世界对象时正在为它分配一个值。
这是我的代码:
import java.util.Scanner;
public class Driver {
public static void main(String args[]) {
//Create a scanner object
Scanner input = new Scanner(System. in );
boolean exit = true;
//Prompt user to enter world width and height
System.out.print("World width: ");
int userWidth = input.nextInt();
System.out.print("World height: ");
int userHeight = input.nextInt();
//Create a world object
World world1 = new World(userWidth, userHeight);
world1.displayWorld();
System.out.println("Possible inputs: up, down, left, right, exit");
while (exit = true) {
//display world
world1.displayWorld();
System.out.print("ACTION > ");
String userAction = input.next();
if (userAction == "up" || userAction == "down" || userAction == "left" || userAction == "right" || userAction == "exit") {
switch (userAction) {
case "up":
world1.moveUp();
break;
case "down":
world1.moveDown();
break;
case "left":
world1.moveLeft();
break;
case "right":
world1.moveRight();
break;
case "exit":
exit = false;
break;
}
} else {
System.out.println("Invalid Input. Possible inputs are: up, down, left, right, or exit.");
}
}
}
}
class World {
static private char[][] worldDimensions;
static private int characterRow;
static private int characterColumn;
public World(int userWidth, int userHeight) {
char[][] worldDimensions = new char[userHeight][userWidth];
int characterRow = 0;
int characterColumn = 0;
for (int row = 0; row < worldDimensions.length; row++) {
for (int column = 0; column < worldDimensions[row].length; column++) {
if (characterRow == row && characterColumn == column) {
worldDimensions[row][column] = (char)
'P';
} else {
worldDimensions[row][column] = (char)
'-';
}
}
}
}
public void displayWorld() {
for (int row = 0; row < worldDimensions.length; row++) {
for (int column = 0; column < worldDimensions[row].length; column++) {
System.out.print(worldDimensions[row][column]);
}
System.out.println();
}
}
public void moveUp() {
if (characterRow - 1 >= 0) {
characterRow--;
}
}
public void moveDown() {
if (characterRow + 1 <= worldDimensions[0].length) {
characterRow++;
}
}
public void moveLeft() {
if (characterColumn - 1 >= 0) {
characterColumn--;
}
}
public void moveRight() {
if (characterRow + 1 <= worldDimensions.length) {
characterRow++;
}
}
}
最佳答案
您正在构造器中隐藏worldDimensions
。本地声明不能为您提供与先前声明相同的字段。
只需删除声明,就可以了(至少对于该错误而言):
worldDimensions = new char[userHeight][userWidth];
07-28 00:01