2014-02-10 97 views
0

作为Java项目入门的一部分,我试图创建一个简单的命令行游戏,但每次在我的角色对象上调用某个方法时都会收到NullPointerException。我没有正确地声明我的对象变量? (错误开始行48 - promptWhichMethod第二条线(字符串CMD))声明的对象变量给NullPointerException(Java)?

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

您只声明了一个变量,但没有为其分配任何值。因此你会得到NPE。 – RaviH

+0

你在哪里调用'promptWhichMethod(String cmd)'这个方法。当你传递'cmd'时,确保'String cmd'不为空 – Babel

+0

你必须实际创建这些对象。 –

回答

2

shadowing变量play1。更换

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

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

同applys到play2

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。