Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: NullPointerException?

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default NullPointerException?

    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?

    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)
    Player Class :

    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!


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: NullPointerException?

    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)
    You read the stack trace in the error message starting at the bottom:
    code at line 83 called the bMain() method
    code at line 14 called the mAttack() method
    code at line 59 called the getBlock() method
    At line 71 there was a variable with a null value.

    To fix the problem, look at line 71, find the variable that had the null value and backtrack in the code to see why that variable did not have a valid value.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. why am I getting NullPointerException.
    By mia_tech in forum What's Wrong With My Code?
    Replies: 9
    Last Post: October 24th, 2012, 11:33 AM
  2. NullPointerException
    By Alket in forum Member Introductions
    Replies: 1
    Last Post: June 7th, 2012, 07:09 AM
  3. NullPointerException
    By deathmatex in forum Exceptions
    Replies: 4
    Last Post: March 27th, 2012, 03:54 AM
  4. [SOLVED] NullPointerException
    By javapenguin in forum What's Wrong With My Code?
    Replies: 13
    Last Post: October 1st, 2010, 12:10 AM
  5. NullPointerException
    By bbr201 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: July 29th, 2010, 07:06 PM

Tags for this Thread