0

I'm getting a Exception in thread "main" java.lang.NullPointerException error in my code.

It's supposed to be a simulation of a Battle between a Player and a Monster. Every two weeks I get an assignment to upgrade it with new features. This week I have to make another version of the Monster: here it takes half damage from normal attacks and one and a half damage from spells. (ChargeMonster.java was another task but it works)

The problem is in ResistantMonster.java, but I don't know what it is so I will include my whole code.

Exception in thread "main" java.lang.NullPointerException
at ResistantMonster.takeDamage(ResistantMonster.java:12)
at Character.attack(Character.java:48)
at Game.main(Game.java:161)

Code:

public class ResistantMonster extends Monster {

    public ResistantMonster(int maxHp, int atk, double hitChance) { 
        super(maxHp, atk, hitChance);
    }

    public void takeDamage(int damage) {
        if (input.equals("1")) { // exception occurs here
            currentHp -= ((int) 0.5 * damage);
        } else { 
            currentHp -= ((int) 1.5 * damage);
        if (getCurrentHp() < 0) {
            currentHp = 0;
        }
    }
}

public class Game {

    public static void main(String[] args) {
        Player player = new Player(150, 30, 60, 3, 0.7, 100, 15); 
        Monster[] monsters = new Monster[5];
        for (int i = 0; i < 5; i++) {
            monsters[i] = new ResistantMonster(100 + (int) (Math.random() * 101), 20 + (int) 
        } 
        Monster monster = monsters[(int) (Math.random() * 5)];

        Scanner sc = new Scanner(System.in); 
        String input;

        int turn = 0;

        while (player.getCurrentHp() > 0 && monster.getCurrentHp() > 0) { 
            System.out.println("----------------------------------------------------------------");
            System.out.println("Round " + turn);
            System.out.println(player.toString());
            System.out.println(monster.toString());
            System.out.println("----------------------------------------------------------------");
            System.out.println("Possible commands:");
            System.out.println("1 -> Attack");
            System.out.println("2 -> Item (" + player.getRemainingItemUses() + " remaining)");
            System.out.println("3 -> Fire-Spell (AP 50)");
            System.out.println("4 -> Water-Spell (AP 40)");
            System.out.println("5 -> Thunder-Spell (AP 30)");
            System.out.println("Which command??");          

            input = sc.nextLine();

            if (input.equals("1")) {
                turn++;
                System.out.println("Player attacks!");
                if ( player.attack(monster) != -1) {  //!!!!errorline
                    System.out.println("Player hits!");
                } else {
                    System.out.println("Player missed.");
                } 
            } else if (input.equals("2")) {
                turn++;
                if ( player.heal(player.getHealingPower())) {
                    System.out.println("Player uses a healing potion.");
                } else {
                    System.out.println("Player has no more potions.");
                }
            } else if (input.equals("3")) {
                turn++;
                if (player.spell1(monster)) {
                    System.out.println("Player uses a fire spell!");
                } else {
                    System.out.println("Player has not enough ability points left.");
                } 
            } else if (input.equals("4")) {
                turn++;
                if (player.spell2(monster)) {
                    System.out.println("Player uses a water spell!");
                } else {
                    System.out.println("Player has not enough ability points left.");
                }
            } else if (input.equals("5")) {
                turn++;
                if (player.spell3(monster)) {
                    System.out.println("Player uses a thunder spell!");
                } else {
                    System.out.println("Player has not enough ability points left.");
                }
            } else {
                turn++;
                System.out.println("Invalid Command");
            }
            player.regenerateAp(); 

            if (monster.isDefeated()) {
                break; 
            }

            System.out.println("Monster attacks!");

            int ret = monster.action(player);
            if ( ret >= 0) {
                System.out.println("Monster hits!");
            } else if( ret == -1) { 
                System.out.println("Monster missed.");
            } else {
                System.out.println("Monster charges.");
            }
        }

        sc.close();
        System.out.println("----------------------------------------------------------------");
        System.out.println("Game over in round " + turn);
        System.out.println(player.toString());
        System.out.println(monster.toString());
        System.out.println("----------------------------------------------------------------");

        if (player.isDefeated()) {
            System.out.println("Player won!");
        } else {
            System.out.println("Player lost!");
        }
    }
}

public class Character {
    protected int currentHp; 
    protected int atk;
    protected double hitChance;

    public Character(int maxHp, int atk, double hitChance) {
        this.currentHp = maxHp;
        this.atk = atk;
        this.hitChance = hitChance;
    }

    public void takeDamage(int damage) { 
        currentHp -= damage;
        if (getCurrentHp() < 0) {
            currentHp = 0;
        }
    }

    public boolean isDefeated() {
        if (getCurrentHp() == 0) {
            return true;
        } else {
            return false;
        }
    }

    public int getAtk() { 
        return this.atk;
    }

    public int getCurrentHp() {
        return this.currentHp;
    }

    public int attack(Character target) {
        if (Math.random() <= hitChance) {
            int damage = (int) (atk * (Math.random() + 1.0));
            target.takeDamage(damage); //!!!!errorline
            return damage;
        } else {
            return -1;
        }
    }

    public String toString() {
        return "HP " + currentHp + " - ATK " + atk + " - Hit Chance " + hitChance;
    }
}

public class Monster extends Character {

    public Monster(int maxHp, int atk, double hitChance) { 
        super(maxHp, atk, hitChance);
    }

    public int action(Character target) {
        return attack(target);
    }

    public String toString() {
        return "Monster: HP " + currentHp + " - ATK " + atk + " - Hit Chance " + hitChance;
    }
}

public class ChargeMonster extends Monster {
    private double chargeChance = 0.2; 

    private boolean charged = false;

    public ChargeMonster(int maxHp, int atk, double hitChance) { // Konstruktor
        super(maxHp, atk, hitChance);
    }

    public int action(Character target) {
        if (!charged) { 
            double rdm = Math.random();
            if ( rdm > chargeChance) {
                return attack(target);
            } else {
                charged = true;
                return -2;
            } 
        } else {
           return attack(target);
        }
    }

    public int attack(Character target) {
        if (!charged) {
            if (Math.random() <= hitChance) {
                int damage = (int) (atk * (Math.random() + 1.0));
                target.takeDamage(damage);
                return damage;
            } else {
                return -1;
            }
        } else {
            if (Math.random() <= hitChance) {
                int damage = (int) (atk * (Math.random() + 2.5));
                target.takeDamage(damage);
                charged = false;
                return damage;
            } else {
                return -1;
            }
        }
    }
}

PS: if needed I can copy my Player.java

PS2: please leave a comment on how my coding is. Any tips for improvement would be really awesome! Thanks in advance!

2
  • its actually P.P.S. :P Commented Jan 15, 2015 at 21:48
  • Go to this site to get a code review: codereview.stackexchange.com (but your code should be working first). Commented Jan 15, 2015 at 22:15

2 Answers 2

2

As per your error, you are attempting to access something which is null. The only thing that you are accessing on the marked line is input. Therefore, your error is telling you that input is null.

Where are you setting input in class ResistantMonster? As far as I can tell by looking at your code, you do not have it anywhere.

To fix this, send in input (what I assume you were attempting to access) from your main class to your instance of ResistantMonster via either the constructor, or the method takeDamage().

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

Comments

0

You are doing quite mistake here you are declaring input variable in main class and trying to access it in the ResistantMonster It impossible

Either you have to declare it static or you have to pass it while calling method attack of ResistantMonster.

Thanks, Himanshu

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.