我的代码中出现Exception in thread "main" java.lang.NullPointerException
错误。
它应该是玩家和怪物之间战斗的模拟。
每两周我都会获得一项使用新功能升级它的任务。
本周我必须制作另一个版本的怪物:这里它受到正常攻击造成的一半伤害和法术造成的一点半伤害。
(ChargeMonster.java
是另一项任务,但它确实有效)
问题出在ResistantMonster.java
,但我不知道它是什么,所以我将包含我的整个代码。
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)
代码:
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:如果需要,我可以复制我的Player.java
PS2:请对我的编码方式发表评论。任何改进的提示都会非常棒!提前谢谢!
答案 0 :(得分:2)
根据您的错误,您正在尝试访问null的内容。您在标记的行上访问的唯一内容是input
。因此,您的错误告诉您input
为空。
您在班级input
设置ResistantMonster
的位置?据我所知,通过查看你的代码,你没有任何地方。
要解决此问题,请通过构造函数或方法input
将ResistantMonster
(我假设您尝试访问的内容)从主类发送到takeDamage()
的实例。
答案 1 :(得分:0)
你在这里做错了你在主类中声明输入变量并试图在ResistantMonster中访问它是不可能的
您必须声明它是静态的,或者在调用ResistantMonster的方法攻击时必须传递它。
谢谢, 人士Himanshu