我无法解决的空指针异常

时间:2014-12-03 13:46:39

标签: java nullpointerexception

我无法修复此空指针异常,也不能修复我的教授。我已经注释掉了一段时间并对它进行了研究,但由于某些原因它不起作用!错误是:空指针异常,如果有帮助我可以发布实际错误

 public static void main(String[] args) {
    // initialize character and world
    Player player = new Player();
    map = new Map();
    Jf = new JFrameManager();
    Jf.addToOutput("Mage or Knight?");
    do {
        player.setPlayerType(Jf.getCommand());
        Jf.setClass(player.getPlayerHeader());
    } while (!("Mage".equalsIgnoreCase(player.getPlayerType()) || "Knight"
            .equalsIgnoreCase(player.getPlayerType())));
    Jf.addToOutput("You Have Selected " + player.getPlayerType() + ", "
            + player.getPlayerName());
    Jf.clearCommand();
    while (true) {
        nextCommand(Jf.getCommand());
    }

}

public static void nextCommand(String commandInput) {
    String command = null;
    String constructor = null;
    String[] str_array;
    if (!commandInput.equals("")) {
        str_array = commandInput.split(" ");
        command = str_array[0];
        constructor = str_array[1];
    } else {
        command = "";
    }
    switch (command.toLowerCase()) {
    case "move":
        if (isValidMove(constructor)) {
            player.move(constructor);
        }
        break;
    case "equipmain":
        player.setWeapon(1, player.getItemNumber(constructor));
        break;
    case "equipoff":
        player.setWeapon(2, player.getItemNumber(constructor));
        break;
    case "equiparmor":
        player.setArmor(player.getItemNumber(constructor));
        break;
    case "trash":
        player.deleteFromInv(constructor);
        break;
    }
}

public static void initiateEventMove(String direction) {
    if (isValidMove(direction)) {
        Jf.addToOutput("You have Moved " + direction);
        player.move(direction);
        switch (map.getEvent(player.getPosition())) {
        case 2:
            intiateAIFight();
            break;
        }
    }

}

public static boolean isValidMove(String direction) {
    boolean r = false;
    switch (direction.toLowerCase()) {
    case "up":
        if ((((player.getPosition() - 20) / 20) >= 1)
                && (map.getEvent(player.getPosition()) != 0)) {
            r = true;
        }
        ;
        break;
    case "down":
        if ((((player.getPosition() + 20) / 20) >= 1)
                && (map.getEvent(player.getPosition()) != 0)) {
            r = true;
        }
        ;
        break;
    case "left":
        if ((((player.getPosition() - 1) % 20) != 0)
                && (map.getEvent(player.getPosition()) != 0)) {
            r = true;
        }
        ;
        break;
    case "right":
        if (((player.getPosition() % 20) != 0)
                && (map.getEvent(player.getPosition()) != 0)) {
            r = true;
        }
        ;
        break;
    }
    return r;
}

public static void intiateAIFight() {
    mob = new AI("mixed", 0, False);
    while ((mob.isDead() != true) && (player.isDead() != true)) {
        mob.takeDamage(player.attack());
        Jf.addToOutput("You gave the " + mob.getType() + player.attack());
        player.takeDamage(mob.attack());
        Jf.addToOutput("You recieved " + mob.attack()
                + " dammage from the " + mob.getType());
    }
}

}

2 个答案:

答案 0 :(得分:0)

有多个地方可以出现nullpointers,但是你发布的信息不足。

我想可能的原因是在你的main方法中你定义了一个新的局部变量:

Player player = new Player();

但是在您的方法nextCommand()中,您尝试访问之前未实例化的全局变量“player”。

例如:

player.setWeapon(1, player.getItemNumber(constructor));

所以我的猜测是你必须改变行

Player player = new Player();

player = new Player();

设置你的全局变量。

下一个原因可能是

nextCommand(Jf.getCommand())

你的Jf.getCommand()返回null,你用参数null调用nextCommand-method。

在该方法中,您将此字符串与另一个字符串进行比较,而不检查它是否为空。

if (!commandInput.equals("")) {
    str_array = commandInput.split(" ");
    command = str_array[0];
    constructor = str_array[1];
}

也可能出现nullpointer异常

如上所述,只有两种可能性。你必须添加stacktrace和完整的类来找到合适的

答案 1 :(得分:0)

代码:

public static void nextCommand(String commandInput) {
    String constructor = null;
    ...
    if (!commandInput.equals("")) {
    ...
    } else {
        command = "";
    }
    ...
    if (isValidMove(constructor)) {

“isValidMove(constructor)”抛出NullPointerException,如果是“commandInput.equals(”“)”,因为在“isValidMove(constructor)”中你使用“direction.toLowerCase()”