TheDetailer
TheDetailer

Reputation: 299

Using 2 objects at once in Java OOP

I am trying to create two objects that fight each other. Both objects (warrior and dragon) are subclasses. My goal is to be able to have one object attack the other object, and show the results. With my current code I keep having exceptions thrown.

import java.util.Random;
import java.util.Scanner;

/**
 *
 * @author Brenton
 */
public class Fighter {

    private String name;
    private int attack;
    private int level = 1;
    private int health = 50;
    private boolean isAlive = true;

    private Fighter fighterOne;
    private Fighter fighterTwo;

    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAttack() {
        Random generator = new Random();
        attack = generator.nextInt(10) * level + 1;
        return attack;
    }

    public void setAttack(int attack) {
        this.attack = attack;
    }

    public int getLevel() {
        return this.level;
    }

    public void setLevel(int level) {
        this.level = level;
    }

    public int getHealth() {
        if(this.health <= 0)
        {
            this.health = 0;
        }
        return this.health;
    }

    public void setHealth(int health) {
        this.health = health;
    }

    public boolean isAlive() {
        if(this.health <= 0)
        {
            this.isAlive = false;
        }
        return this.isAlive;
    }

    public static String getWelcome() {
        String welcome = "Hello and welcome to FightClub, do you wish to fight, yes or no? ";
        return welcome;
    }

    public String getPunch(Fighter fighterTwo) {
        this.fighterTwo = fighterTwo;
        String hit = "You choose to punch the other fighter and dealt" + getAttack() + " your opponent now has " + fighterOne.decreaseHitPoints(fighterTwo) + " remaining";
        return hit;
    }

    public int decreaseHitPoints(Fighter fighterTwo) {
        this.fighterTwo = fighterTwo;
        int health = fighterTwo.getHealth();
        int attack = getAttack();
        health = health - attack;
        return health;
    }

    public static String invalidInput() {
        String invalid = "I am sorry that is not a valid input option ";
        return invalid;
    }

    public void getWinner(Fighter fighterOne, Fighter fighterTwo) {
        this.fighterOne = fighterOne;
        this.fighterTwo = fighterTwo;
        if(fighterOne.isAlive() == false && fighterTwo.isAlive() == false)
        {
            System.out.println("Both fighters have fallen heroically");
        }
        else if(fighterOne.isAlive() == true && fighterTwo.isAlive() == false)
        {
            System.out.println(fighterOne + " is victorious! ");
        }
        else if(fighterOne.isAlive() == false && fighterTwo.isAlive() == true)
        {
            System.out.println(fighterTwo + " is victorious! ");
        }
        else
        {
            System.out.println("ERROR ERROR ERROR");
        }      
    }

    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);

        Fighter a = new Warrior();
        Fighter b = new Dragon();

        System.out.print(getWelcome());     
        while(in.hasNextLine()) 
        {
            switch(in.nextLine()) 
            {
                case "no":
                    System.out.println("Wow, you are not even gonna try, you have lost!");
                    break;
                case "yes":
                    System.out.println("Let the fight begin! ");
                    while(a.isAlive() && b.isAlive()) 
                    {
                        System.out.println("Do you want to punch, kick, or headbutt the other fighter? ");
                        switch(in.nextLine()) 
                        {
                            case "punch":
                                System.out.println(a.getPunch(b));
                                break;
                            /*case "kick":
                                System.out.println(a.getKick(b));
                                break;
                            case "headbutt":
                                System.out.println(a.getHeadbutt(b));
                                break;*/
                            default :
                                System.out.println(invalidInput());
                                break;
                        }
                    }
                default:
                    System.out.println(invalidInput());
                    break;  
            }//end of first switch statement
        }//end of first while loop
    }//end of main   
}

This is the error

Exception in thread "main" java.lang.NullPointerException
    at Dragonslayer.Fighter.getPunch(Fighter.java:79)
    at Dragonslayer.Fighter.main(Fighter.java:140)
Java Result: 1

Upvotes: 0

Views: 78

Answers (1)

Rustam
Rustam

Reputation: 6515

In your getPunch(...) method

replace

fighterOne.decreaseHitPoints(fighterTwo) // here fighterOne can be null

with

this.decreaseHitPoints(fighterTwo)  // use this which is nothing but your invoking object a.

Upvotes: 1

Related Questions