Hello all! I JUST started coding java and I was just practicing and I came across an error:
Exception in thread "main" java.lang.NullPointerException
at fightGame.startFight(fightGame.java:26)
at launchGame.main(launchGame.java:8)
Soooo... I'm making a stab here and guessing that the error is on line 26 of my fightGame method. Ill post that below:
public class fightGame { player fighter1; player fighter2; public void introFight() { player fighter1 = new player(); player fighter2 = new player(); fighter1.sex = 'M'; fighter2.sex = 'M'; fighter1.name = "Lig"; fighter2.name = "Bone"; fighter1.health = 100; fighter2.health = 100; System.out.println("Welcome one and all to the grand fighting arena!"); System.out.println("Today we will see the fight off between the one and only: " + fighter1.name + " and " + fighter2.name); System.out.println("\n"); } public void startFight() { while ((fighter1.health > 0) | (fighter2.health > 0)) { fighter1.attack(); fighter2.attack(); System.out.println(fighter1.name + "'s health is now: " + fighter1.health); System.out.println(fighter2.name + "'s health is now: " + fighter2.health); } if (fighter1.health <= 0){ fighter1.die(); } else{ fighter2.die(); } } }
I just can't figure out what is wrong with that line of code, so any help or tips or suggestions would be awesome!
Sorry if it is a real obvious problem!
Thanks so much!!!
--- Update ---
*Woops! Meant in my startFight method!