【问题标题】:Declared object variables giving NullPointerException (Java)?声明的对象变量给出 NullPointerException (Java)?
【发布时间】:2014-02-10 11:28:51
【问题描述】:

作为 Java 入门项目的一部分,我正在尝试创建一个简单的命令行游戏,但每次在我的角色对象上调用方法时都会收到 NullPointerException。我没有正确声明我的对象变量吗? (错误从第 48 行开始 - promptWhichMethod(String cmd) 的第 2 行)

package battle; 
import java.io.*;

public class Battle{
public int play1Ap;
    public int play2Ap;
    public Character play1;
public Character play2;
private boolean playing;
public static void main(String[] args){
    Battle game = new Battle();
    game.run();
}
public Battle(){    
    Character play1 = new Athlete("Ellisaville", "President");
    Character play2 = new Medic("Amberland", "Lieutenant"); 
}
public void run(){
    playing = true;
    welcomeAndExplain();
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    while(playing == true){
        try {
                String cmd = br.readLine();
                promptWhichMethod(cmd);
        } catch (IOException e) {
                System.out.println("Invalid command. Remember, the commands are: Bomb, Bio, and Famine");
        }
    }
}
public void welcomeAndExplain(){
    System.out.println("Welcome! The point of this game is to capture your opponents land before they capture yours!");
    System.out.println("Through the use of these the commands: Bomb, Bio, Famine - and some strategy, you can become victor!");
    System.out.println("Some methods have a chance to cause more damage than others, but they also use more stamina. If you deplete your stamina, you'll be unable to attack!");
    System.out.println("Here are some useful stats: ");
    System.out.println("Bomb - Takes up to 6 points (w/o bonus) from enemy health. Uses 3 stamina");
    System.out.println("Bio - Takes up to 3 points (w/o bonus) from enemy health. Uses 2 stamina");
    System.out.println("Famine - Takes up to 2 points (w/o bonus) from enemy health. Uses 1 stamina");
    System.out.println("Base stats for each character (w/o bonuses) are 10 health and 3 stamina");
    System.out.println("Once depleted, stamina takes 2 turns to replenish");
    System.out.println("To attack, first type Play1/Play2 to indicate which player is attacking, then type your method");
    System.out.println("Attacks by warlords take 2 bonus health points from enemy. Athletes have 2 bonus stamina points. Medics get 2 bonus health points");
    System.out.println("Type 'End' to end the game.");
}

public void promptWhichMethod(String cmd){
    String[] commands = cmd.split(" ");
    play1Ap = play1.getExtraAp();
    play2Ap = play2.getExtraAp();
    if(commands[0].equals("Play1") && play1.checkHasStamina() == true){
        if(commands[1].equals("Famine")){
            play1.attackFamine();
            play2.beAttackFamine(play1Ap);
        }else if(commands[1].equals("Bio")){
            play1.attackBioWarfare();
            play2.beAttackBioWarfare(play1Ap);
        }else if(commands[1].equals("Bomb")){
            play1.attackBomb();
            play2.beAttackBomb(play1Ap);
        }
    } else if(commands[0].equals("Play2") && play2.checkHasStamina()){
        if(commands[1].equals("Famine")){
            play2.attackFamine();
            play1.beAttackFamine(play2Ap);
        }else if(commands[1].equals("Bio")){
            play2.attackBioWarfare();
            play1.beAttackBioWarfare(play2Ap);
        }else if(commands[1].equals("Bomb")){
            play2.attackBomb();
            play1.beAttackBomb(play1Ap);
        }
    } else if(commands[0].equals("End")){
        System.out.println("Done game");
        playing = false;
    }else {
        System.out.println("invalid command or not enough stamina");
    }
}
}

【问题讨论】:

  • 你只声明了一个变量,没有给它赋值。因此,您将获得 NPE。
  • 你在哪里调用promptWhichMethod(String cmd)这个方法。当你传入cmd 时,确保String cmd 不为空
  • 您必须实际创建对象。

标签: java nullpointerexception


【解决方案1】:

你是 shadowing 变量 play1。替换

Character play1 = new Athlete("Ellisaville", "President");

play1 = new Athlete("Ellisaville", "President");

同样适用于play2

【讨论】:

    【解决方案2】:

    问题来了:

    Character play1 = new Athlete("Ellisaville", "President");
    Character play2 = new Medic("Amberland", "Lieutenant"); 
    

    这样,play1 和 play2 的范围就限制在你的构造函数中了。

    这应该是:

    play1 = new Athlete("Ellisaville", "President");
    play2 = new Medic("Amberland", "Lieutenant"); 
    

    只要您在类级别(您是)声明 play1 和 play2,那么构造函数中的上述两行将让您的类的其余部分“看到”play1 和 play2。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-03
      • 2013-11-11
      • 2012-02-09
      • 1970-01-01
      • 2016-07-28
      • 2014-09-03
      相关资源
      最近更新 更多