声明的对象变量给出NullPointerException(Java)?

时间:2014-02-10 03:20:46

标签: java nullpointerexception

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

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

2 个答案:

答案 0 :(得分:2)

shadowing变量play1。取代

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

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

同样适用于play2

答案 1 :(得分:0)

问题在于:

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。