I've got a lot of code, and I'm not entirely sure what I've done wrong. My IDE isn't showing any errors, but I've got NullPointerExceptions in three of my classes.
What should I do?
Player Class :Exception in thread "main" java.lang.NullPointerException
at rpgtxt.Player.getBlock(Player.java:71)
at rpgtxt.Battle.mAttack(Battle.java:59)
at rpgtxt.Battle.bMain(Battle.java:14)
at rpgtxt.Main.main(Main.java:83)
package rpgtxt; import java.util.*; public class Player { public int hp; public int hptotal; public int dmg; public int str; public int intlg; public int will; public int xp; public int lvl; public int block; public String pCls; public boolean isAlive; public String pName() { Scanner scn = new Scanner(System.in); String ans = scn.nextLine(); ans = pCls; return ans; } public String pClass() { Scanner scn = new Scanner(System.in); String ans = scn.nextLine(); return ans; } public int getHP() { hp = getLVL() * 25; return hp; } public int getLVL() { int xp_total = xp; if (xp_total >= 110000) { lvl = 5; return lvl; } else if (xp_total >= 75000) { lvl = 4; return lvl; } else if (xp_total >= 45000) { lvl = 3; return lvl; } else if (xp_total >= 20000) { lvl = 2; return lvl; } else { lvl = 1; return lvl; } } public int getBlock() { if (pCls.equalsIgnoreCase("Warrior")) { block = str - 10; } else if (pCls.equalsIgnoreCase("Rogue")) { block = will - 10; } else if (pCls.equalsIgnoreCase("Wizard")) { block = intlg - 10; } return block; } }
Main Class:
package rpgtxt; import java.util.*; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); Player player = new Player(); int lvldsp = player.getLVL(); System.out.println("Hello. What's your name?"); String Name; Name = player.pName(); System.out.println("Ah... " +Name+ ". Are you a rogue, warrior, or wizard?"); String Class = player.pClass(); int clsslct = 1; while (clsslct == 1) { if (Class.equalsIgnoreCase("Rogue")) { player.str = 5; player.intlg = 10; player.will = 15; clsslct--; } else if (Class.equalsIgnoreCase("Wizard")) { player.str = 5; player.intlg = 15; player.will = 10; clsslct--; } else if (Class.equalsIgnoreCase("Warrior")) { player.str = 15; player.intlg = 5; player.will = 10; clsslct--; } } System.out.println("The path of the " +Class+ " is a noble one."); if (Class.equalsIgnoreCase("Warrior")) { System.out.println("Here's a sword."); System.out.println("You received: Wooden Sword! Str + 1"); Item wsword = new Item(); wsword.iStr = 1; player.dmg = 1 + wsword.iStr + player.str; wsword.iName = "Wooden Sword"; System.out.println("Your damage is now "+player.dmg); } else if (Class.equalsIgnoreCase("Rogue")) { System.out.println("Here are two daggers."); System.out.println("You received: Wooden Daggers! Will + 1"); Item wdags = new Item(); wdags.iWil = 1; player.dmg = 1 + wdags.iWil + player.will; wdags.iName = "Wooden Daggers"; System.out.println("Your damage is now "+player.dmg); } else if (Class.equalsIgnoreCase("Wizard")) { System.out.println("Here's a staff."); System.out.println("You received: Wooden Staff! Int + 1"); Item wstaff = new Item(); wstaff.iInt = 1; player.dmg = 1 + wstaff.iInt + player.intlg; wstaff.iName = "Wooden Staff"; System.out.println("Your damage is now "+player.dmg); } System.out.println("Now that you have a weapon, are you prepared for your first battle?"); String fBattle; fBattle = in.nextLine(); if (fBattle.equalsIgnoreCase("yes")) { Monster fMon = new Monster(); fMon.mName = "Zombie"; Battle rBattle = new Battle(); rBattle.bMain(player, fMon); } if (player.isAlive = false) { System.out.println("Game over!"); } } }
Battle Class:
package rpgtxt; import java.util.*; public class Battle { public void bMain(Player player, Monster monster) { Scanner keyIn = new Scanner(System.in); System.out.println("A " +monster.mName+ " has attacked!"); do { wrAttack(player, monster); keyIn.nextLine(); mAttack(player, monster); } while(player.hp > 0 && monster.hp > 0); } public void wrAttack(Player player, Monster monster) { Scanner keyIn = new Scanner(System.in); System.out.println("You attack!"); Random roll = new Random(); int rvalue = roll.nextInt( 21 ); int mBlock = monster.getBlock(player); System.out.println(monster.mName+" has "+monster.hp+" hp."); keyIn.nextLine(); if (rvalue == 20) { System.out.println("Critical hit!"); monster.hp = monster.hp - (player.dmg * 2); System.out.println(monster.mName+" has "+monster.hp+" hp."); player.xp = player.xp + 1000; keyIn.nextLine(); } else if (rvalue > mBlock) { System.out.println("You strike!"); monster.hp = monster.hp - player.dmg; System.out.println(monster.mName+" has "+monster.hp+" hp."); player.xp = player.xp + 500; keyIn.nextLine(); } else if (rvalue < mBlock) { System.out.println("You miss!"); keyIn.nextLine(); } } public void mAttack(Player player, Monster monster) { Scanner keyIn = new Scanner(System.in); System.out.println(monster.mName+" attacks!"); Random roll = new Random(); int rvalue = roll.nextInt( 21 ); int block = player.getBlock(); System.out.println("You have "+player.hp+" hp."); keyIn.nextLine(); if (rvalue == 20) { System.out.println("Critical hit!"); player.hp = player.hp - (monster.dmg * 2); System.out.println("You have "+player.hp+" hp."); keyIn.nextLine(); } else if (rvalue > block) { System.out.println(monster.mName+" strikes!"); player.hp = player.hp - monster.dmg; System.out.println("You have "+player.hp+" hp."); keyIn.nextLine(); } else if (rvalue < block) { System.out.println(monster.mName+" misses!"); keyIn.nextLine(); } } }
Any help is appreciated!