0

As part of an intro-to-Java project, I'm trying to create a simple command line game, but am getting a NullPointerException each time I call a method on my character objects. Am I not declaring my object variables correctly? (Errors start on line 48 - 2nd line of 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");
    }
}
}
3
  • You have only declared a variable, but not assigned any value to it. Therefore you will get NPE. Commented Feb 10, 2014 at 3:23
  • Where are you calling promptWhichMethod(String cmd) this method . when you are passin cmd make sure String cmd is not null Commented Feb 10, 2014 at 3:24
  • You have to actually create the objects. Commented Feb 10, 2014 at 3:28

2 Answers 2

2

You're shadowing the variable play1. Replace

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

with

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

The same applys to play2

Sign up to request clarification or add additional context in comments.

Comments

0

Here is the problem:

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

With this, the scope of play1 and play2 is limited to your constructor.

This should be:

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

As long as you declare play1 and play2 at the class level (which you are), then the above two lines in the constructor will let the rest of your class 'see' play1 and play2.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.