-1

I keep getting this error in my code. Can someone fix it and how is the code written? Can it be improved by maybe using setters and getters only?

Exception in thread "main" java.lang.NullPointerException
at Player.attack(Player.java:72)
at Main.main(Main.java:15)

My code: Player.java

public class Player {
String name;
String race;
int hp;
int power;
int armour;
Weapon weapon;


public Player (String n, String r, int h, int p, int a) {
name = n;
race =r;
hp =  h;
power = p;
armour = a;


}

public void setName (String n) {

name = n;
}

public String getName() {
return name;
}
public void setRace (String r) {
race = r;
}
public String getRace() {
return race;
}
public void setHP (int h) {
hp = h;
}
public int getHP() {
return hp;
}
public void setPower (int p) {
power = p;
}
public int getPower() {
return power;
}
public void setArmour (int a) {
armour = a;
}
public int getArmour() {
return armour;
}

public boolean dead() {
return hp <= 0;
}
public boolean equip(Weapon weapon) {
this.weapon = weapon;
return true;
}
public boolean receiveDamage(int i) {
if ((hp - i) > 0) {
    hp = hp - i;
    return true;
    }
hp = 0;
return false;

}
public boolean attack(Player player) {

return player.receiveDamage(weapon.useWeapon());

}
}

Main.java

public class Main {
public static void main(String args[]) {
Player Mensch = new Player("Mensch", "Mensch", 85, 12, 10);
Player Ork = new Player("Shrek", "Ork", 50, 14, 6);

Weapon MenschW = new Weapon("mächtiges Schwert", 15, 100);
Weapon OrkW = new Weapon("große Axt", 7, 100);

Mensch.equip(Mensch.weapon);
Ork.equip(Ork.weapon);

while (!Mensch.dead() && !Ork.dead() ) {    //Alternativ: for (player hp >=0)

    System.out.println("Mensch gegen Ork " + Mensch.attack(Ork));

    if (Mensch.dead() || Ork.dead()) {
        break;
    }

    System.out.println("Mensch gegen Ork " + Ork.attack(Mensch));
}

System.out.println("Ork ist tot: " + Ork.dead());
System.out.println("Mensch ist tot: " + Mensch.dead());

}

}

Weapon.java

import java.util.concurrent.ThreadLocalRandom;
public class Weapon {
String name;
int damage;
int hp;

public Weapon(String string, int d, int hp) { 
    // TODO Auto-generated constructor stub
} 
public void setName (String n) {
    name = n;
}
public String getName() {
    return name;
}
public void setDamage (int d) {
    damage = d;
}
public int getDamage() {
    return damage;
}
public void setWHP (int h) {
    hp = h;
}
public int getWHP() {
    return hp;
}





public int useWeapon() { 
    if 
    (broken())
        return 0;
    hp = hp - 5;
    return (damage / 2) + random();
}
private int random() {
    return ThreadLocalRandom.current().nextInt(1, damage + 1);
}

private boolean broken() {
    return hp <= 0;
}
}

I know its a lot of code but I keep getting the same error, also I'm quite new to java so I would appreciate some tips or suggestions to make my code better or more failsave. The code doesn't do much yet but it will (hopefully) be a simple game soon in which two characters fight eachother with some calculations on damageoutput of each player. In this case a Human and Ork. Feel free to try it out

jmarkmurphy
  • 9,829
  • 28
  • 51

1 Answers1

0

Change

Mensch.equip(Mensch.weapon); // Mensch.weapon is not initialized in constructor so it is null.
Ork.equip(Ork.weapon); // Ork.weapon is not initialized in constructor so it is null as well.

To

// Use your newly created weapons in the main instead.
Mensch.equip(MenschW ); 
Ork.equip(OrkW);
Falla Coulibaly
  • 709
  • 11
  • 21
  • 1
    Using setters and getters only might have helped this mistake. So might following the Java naming/capitalization conventions. – markspace Jan 21 '18 at 04:03