Java 2D游戏NullPointerException

Java 2D游戏NullPointerException,java,Java,我正在尝试用Java创建一个2D游戏,其中提示用户输入行宽和列长。将创建一个打印时看起来像这样的“世界”对象(其中P是玩家的角色): 然后,用户可以输入up、down、left、right或exit,这将使p四处移动,如果移动会将角色移出地图,则什么也不做。我的问题是在输入宽度和长度后,出现以下错误: java.lang.NullPointerException at World.displayWorld(Driver.java:90) at Driver.main(Driver.java:28

我正在尝试用Java创建一个2D游戏,其中提示用户输入行宽和列长。将创建一个打印时看起来像这样的“世界”对象(其中P是玩家的角色):

然后,用户可以输入up、down、left、right或exit,这将使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++;
        }
    }
}
import java.util.Scanner;
公务舱司机{
公共静态void main(字符串参数[]){
//创建扫描仪对象
扫描仪输入=新扫描仪(System.in);
布尔退出=真;
//提示用户输入世界宽度和高度
系统输出打印(“世界宽度:”);
int userWidth=input.nextInt();
系统输出打印(“世界高度:”);
int userHeight=input.nextInt();
//创建世界对象
world1=新世界(用户宽度、用户高度);
world1.displayWorld();
System.out.println(“可能的输入:向上、向下、向左、向右、退出”);
while(exit=true){
//显示世界
world1.displayWorld();
系统输出打印(“操作>”);
字符串userAction=input.next();
如果(userAction==“向上”| | userAction==“向下”| | userAction==“左”| | userAction==“右”| | userAction==“退出”){
开关(用户操作){
案例“up”:
world1.moveUp();
打破
案例“向下”:
world1.moveDown();
打破
案例“左”:
world1.moveLeft();
打破
案例“权利”:
world1.moveRight();
打破
案例“退出”:
退出=假;
打破
}
}否则{
System.out.println(“无效输入。可能的输入有:向上、向下、向左、向右或退出”);
}
}
}
}
阶级世界{
静态私有字符[][]worldDimensions;
静态私有int字符行;
静态私有int字符列;
公共世界(int userWidth、int userHeight){
char[][]worldDimensions=新的char[userHeight][userWidth];
int characterRow=0;
int characterColumn=0;
对于(int row=0;row=0){
字符行--;
}
}
公共无效向下移动(){
如果(字符行+1=0){
字符列--;
}
}
公权{

如果(characterRow+1您正在构造函数内部隐藏
worldDimensions
。本地声明不会提供与先前声明相同的字段

只需删除声明,您就可以了(至少对于该错误):


您正在覆盖类变量worldDimensions

public World(int userWidth, int userHeight)
{
//here is where you were overwriting the global variable, leaving it null
//and populating the local one instead
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)'-';
            }
        }
    }
}
公共世界(int-userWidth、int-userHeight)
{
//这里是覆盖全局变量的地方,将其保留为空
//并将其填充到本地
worldDimensions=新字符[userHeight][userWidth];
int characterRow=0;
int characterColumn=0;
对于(int row=0;row
使用。同时检查以备将来参考,指出空指针在哪一行很有用,而不是我们必须找到它。这里至少有三个错误-使用
=
而不仅仅是
退出
,使用
=
比较字符串,以及冗余转换为
char
…更改“char[][]worldDimensions=…”表示世界类第一行的“worldDimensions=…”。您可以去掉switch语句周围的if-else语句,只添加一个
default
大小写。
worldDimensions = new char[userHeight][userWidth];
public World(int userWidth, int userHeight)
{
//here is where you were overwriting the global variable, leaving it null
//and populating the local one instead
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)'-';
            }
        }
    }
}