Results 1 to 15 of 15
  1. #1
    OxigEN. is offline Member
    Join Date
    Nov 2014
    Posts
    28
    Rep Power
    0

    Default Can't Seem to Figure Out What Causes The Problem

    Well, I'm getting a NullPointerException type of error, I don't know wth it is nor what causes it, I've spent like twenty minutes but I didn't manage to find where the problem occurs.
    Here's the full code.



    Java Code:
    import java.util.Scanner;
    
    
    public class Main {
    
    	
    	//Scanner(s)
    	static Scanner getInput = new Scanner(System.in);
    	
    	//Name
    	static String charName;
    	
    	//Stats
    	static int attack;
    	static int health;
    	
    	//Enemy Variables
    	static int monsterHP;
    	static int monsterAttackDMG;
    	
    	//Difficulty
    	static String difficulty;
    	
    	
    	public static void main(String[] args) {
    		
    		System.out.println("Welcome to Strike!\nPlease enter your character's name:");
    		charName = getInput.next();
    		
    		System.out.printf("\nSo your character name's %s\n\nPlease choose the difficulty of the game.\nWrite either EASY or HARD.\n", charName);
    		if(getInput.hasNextInt()) {
    			System.out.println("Please enter either EASY or HARD.");
    		}
    		else getDifficulty(getInput.next());
    		
    		//Setup has been done, continuing the game.
    	}
    	
    	public static String getDifficulty(String diff) {
    		if(diff.equalsIgnoreCase("HARD")) {
    			System.out.println("You've set the difficulty to HARD!");
    			System.out.println("Alright then " + charName + " write RANDOMIZE to randomize your stats.\nYou're getting closer to fighting the monster!");
    			if(getInput.hasNextInt()){ 
    				System.out.println("Please enter RANDOMIZE to randomize your stats or QUIT to quit the game.");
    			} else {
    				randCheck(getInput.next());
    			}
    			return difficulty = "HARD";
    		} else if(diff.equalsIgnoreCase("EASY")) {
    			System.out.println("You've set the difficulty to EASY!");
    			System.out.println("Alright then " + charName + " write RANDOMIZE to randomize your stats.\nYou're getting closer to fighting the monster!");
    			if(getInput.hasNextInt()) {
    				System.out.println("Please enter RANDOMIZE to randomize your stats or QUIT to quit the game.");
    			} else {
    				randCheck(getInput.next());
    			}
    			return difficulty = "EASY";
    		}
    		else System.out.println("Please ENTER HARD or EASY only.");
    		return diff;
    	}
    	
    	public static String swingResponse(String response) {
    		int damageDone = 0;
    		if(difficulty.equalsIgnoreCase("HARD")) {
    			if(response.equalsIgnoreCase("SWING")) {
    				monsterHP = 50;
    				while(monsterHP > 0) {
    					damageDone = (int) (Math.random() * attack);
    					if(damageDone < 3) { 
    						damageDone = (int) (Math.random() * attack);
    					} else if(monsterHP == 0) {
    						System.out.println("The monster striked you and dealt " + monsterAttackDMG + " -- HP left: " + health);
    						continue;
    					}
    					else {	
    						System.out.println("You swung at the monster! Damage done: " + damageDone);
    						monsterHP -= damageDone;
    						System.out.println("\nMonster HP left: " + monsterHP);
    						monsterAttackDMG = damageDone - 3;
    						health -= monsterAttackDMG;
    						System.out.println("The monster striked you and dealt " + monsterAttackDMG + " -- HP left: " + health);
    					}
    				}
    			}	
    		} else if(difficulty.equalsIgnoreCase("EASY")) {
    			if(response.equalsIgnoreCase("SWING")) {
    				monsterHP = 50;
    				while(monsterHP > 0) {
    					damageDone = (int) (Math.random() * attack);
    					if(damageDone < 2) { 
    						damageDone = (int) (Math.random() * attack);
    					} else if(monsterHP == 0) {
    						System.out.println("The monster striked you and dealt " + monsterAttackDMG + " -- HP left: " + health);
    						continue;
    					}
    					else {	
    						System.out.println("You swung at the monster! Damage done: " + damageDone);
    						monsterHP -= damageDone;
    						System.out.println("\nMonster HP left: " + monsterHP);
    						monsterAttackDMG = damageDone - 2;
    						health -= monsterAttackDMG;
    						System.out.println("The monster striked you and dealt " + monsterAttackDMG + " -- HP left: " + health);
    					}
    					System.out.println("Congratulations! You've killed the monster!\n" + charName + " You're the great warrior!");
    				} 
    			}	
    		}
    
    		return response;
    	}
    	
    	public static void ending() {
    		System.out.println("\n\nThanks for playing Striker! Created by: LaNguAx.\nInspired by: Derek Banas.");
    	}
    	
    	public static String randCheck(String check) {
    		if(check.equalsIgnoreCase("RANDOMIZE")) {
    			System.out.println("Your stats have been randomized!");
    			System.out.println("STATS: Health " + randomizeHEALTH() + "\t Attack " + randomizeATTACK());
    			
    			System.out.println("**You began walking throughout the forest.\nYou suddenly spot the monster you're looking to hunt!\nWhat'll you do?!\nRun or Swing at the MONSTER?!**\n((Write SWING or RUN, The monster has 50 HP.))");
    			swingResponse(getInput.next());
    			
    		} else if(check.equalsIgnoreCase("QUIT")) System.out.println("You've decided to quit the game!");
    		else System.out.println("Please ENTER only RANDOMIZE or QUIT.");
    		return check;
    	}
    	
    	public static int randomizeATTACK() {
    		int rand = (int) (Math.random() * 21);
    		while(rand < 5) {
    			rand = (int) (Math.random() * 21);
    		}
    		return attack = rand;
    	}
    	
    	public static int randomizeHEALTH() {
    		int rand = (int) (Math.random() * 51);
    		while(rand < 20) {
    			rand = (int) (Math.random() * 51);
    		}
    		return health = rand;
    	}
    }

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,143
    Rep Power
    14

    Default Re: Can't Seem to Figure Out What Causes The Problem

    What line throws the Exception? What are the values of every variable on that line? Which one is null?

    Use a debugger or add some print statements to figure that out.
    How to Ask Questions the Smart Way
    Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
    Static Void Games forum - Come say hello!

  3. #3
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Can't Seem to Figure Out What Causes The Problem

    Okay, but the most important thing is the exception you get, complete with the stacktrace. That stacktrace will tell you exactly which line of the code is causing the exception.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,143
    Rep Power
    14

    Default Re: Can't Seem to Figure Out What Causes The Problem

    Right- I should have been more clear.

    Step 1: look at the stack trace to figure out what line throws the NPE.
    Step 2: figure out the values of every variable on that line. One of them is null.
    Step 3: work backwards to figure out why that variable is null.
    Step 4: fix the thing that makes that variable null.
    How to Ask Questions the Smart Way
    Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
    Static Void Games forum - Come say hello!

  5. #5
    OxigEN. is offline Member
    Join Date
    Nov 2014
    Posts
    28
    Rep Power
    0

    Default Re: Can't Seem to Figure Out What Causes The Problem

    Here are the lines that the problem occurs on:

    Exception in thread "main" java.lang.NullPointerException
    at Main.swingResponse(Main.java:66)
    at Main.randCheck(Main.java:124)
    at Main.getDifficulty(Main.java:47)
    at Main.main(Main.java:35)

    What that means? That those values have been changed to null somehow?

  6. #6
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,143
    Rep Power
    14

    Default Re: Can't Seem to Figure Out What Causes The Problem

    Well, look at line 66. What variables are on that line?

    What are the values of each of those variables? Use a debugger or some print statements to figure that out.

    One of them is null. Which one? Why is it null? Work backwards to figure out why you never set its value.
    How to Ask Questions the Smart Way
    Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
    Static Void Games forum - Come say hello!

  7. #7
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Can't Seem to Figure Out What Causes The Problem

    Quote Originally Posted by OxigEN. View Post
    Here are the lines that the problem occurs on:

    Exception in thread "main" java.lang.NullPointerException
    at Main.swingResponse(Main.java:66)
    at Main.randCheck(Main.java:124)
    at Main.getDifficulty(Main.java:47)
    at Main.main(Main.java:35)

    What that means? That those values have been changed to null somehow?
    What Kevin says. But on top of that I'd like to clarify: a NullPointerException means exactly this.

    Java Code:
    String something = getInput.next();
    
    System.out.println(something.length());
    Now if getInput.next() actually returns a String object, no problem. But if getInput.next() returns a null value then 'something' ends up being null and calling something.length() will cause a NullPointerException. If I very plainly describe it (which is not ever 100% accurate), you 'call a method on an object reference which is null'.

    I can say a whole lot more at this point but I'm not going to - first digest what you've been given so far and perhaps you'll draw some correct conclusions from that yourself :)
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  8. #8
    OxigEN. is offline Member
    Join Date
    Nov 2014
    Posts
    28
    Rep Power
    0

    Default Re: Can't Seem to Figure Out What Causes The Problem

    Mhhm-.. I've tried to debug the code as you guys said, it appears difficulty = null.
    I'll try to fix it and see why's that like that.
    I'll edit this comment once I'm done.

    Ha! I've found the solution, it appears that I've declared 'difficulty' outside of the inner 'if' sections in the getDifficulty method.
    Thanks for the help everyone!
    Last edited by OxigEN.; 03-17-2015 at 04:55 PM.

  9. #9
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Can't Seem to Figure Out What Causes The Problem

    Conversationally:

    Java Code:
    return difficulty = "HARD";
    Am I the only one that gets a twitchy eye when reading this code?
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  10. #10
    OxigEN. is offline Member
    Join Date
    Nov 2014
    Posts
    28
    Rep Power
    0

    Default Re: Can't Seem to Figure Out What Causes The Problem

    Quote Originally Posted by gimbal2 View Post
    Conversationally:

    Java Code:
    return difficulty = "HARD";
    Am I the only one that gets a twitchy eye when reading this code?
    At first it was return difficulty = diff;
    but I changed it once I was debugging.

  11. #11
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,143
    Rep Power
    14

    Default Re: Can't Seem to Figure Out What Causes The Problem

    Quote Originally Posted by gimbal2 View Post
    Conversationally:

    Java Code:
    return difficulty = "HARD";
    Am I the only one that gets a twitchy eye when reading this code?
    Nope. In fact, it's a violation of the standard Java coding conventions: section 10.4, to be exact! /nerd

    Code Conventions for the Java Programming Language: 10. Programming Practices
    How to Ask Questions the Smart Way
    Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
    Static Void Games forum - Come say hello!

  12. #12
    OxigEN. is offline Member
    Join Date
    Nov 2014
    Posts
    28
    Rep Power
    0

    Default Re: Can't Seem to Figure Out What Causes The Problem

    Quote Originally Posted by KevinWorkman View Post
    Nope. In fact, it's a violation of the standard Java coding conventions: section 10.4, to be exact! /nerd

    Code Conventions for the Java Programming Language: 10. Programming Practices
    So-... Is it bad or something?

  13. #13
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,143
    Rep Power
    14

    Default Re: Can't Seem to Figure Out What Causes The Problem

    Quote Originally Posted by OxigEN. View Post
    So-... Is it bad or something?
    Well, yeah, a little. It makes your code slightly harder to read, which makes it harder for other people (or even yourself) to understand and maintain. It might not seem like a big deal, but following the standard conventions is a good habit to get into. Lines like yours:

    Java Code:
    return difficulty = diff;
    Should be split up into multiple lines, to make it easier to read:

    Java Code:
    difficulty = diff;
    return difficulty;
    How to Ask Questions the Smart Way
    Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
    Static Void Games forum - Come say hello!

  14. #14
    OxigEN. is offline Member
    Join Date
    Nov 2014
    Posts
    28
    Rep Power
    0

    Default Re: Can't Seem to Figure Out What Causes The Problem

    Quote Originally Posted by KevinWorkman View Post
    Well, yeah, a little. It makes your code slightly harder to read, which makes it harder for other people (or even yourself) to understand and maintain. It might not seem like a big deal, but following the standard conventions is a good habit to get into. Lines like yours:

    Java Code:
    return difficulty = diff;
    Should be split up into multiple lines, to make it easier to read:

    Java Code:
    difficulty = diff;
    return difficulty;
    Oh, so the code itself doesn't bring any problems but the way it's written just makes it harder to read?
    I'll try to write stuff in a bit 'prettier' way then, thanks.

  15. #15
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Can't Seem to Figure Out What Causes The Problem

    You also need to get rid of your static variables and methods. Do it now before it becomes a habit. And your methods both return a value and set a value. Some of your getters return a value which you don't use (but rely on the static value that is set). Why do both? Your randCheck method simply returns the argument that was passed. No need to do that either. finally, you should always use {}, even for single statements at is makes it easier to read and can help avoid coding errors. So

    Java Code:
    public static String randCheck(String check) {
        if(check.equalsIgnoreCase("RANDOMIZE")) {
            System.out.println("Your stats have been randomized!");
            System.out.println("STATS: Health " + randomizeHEALTH() + "\t Attack " + randomizeATTACK());
                 
            System.out.println("**You  began walking throughout the forest.\nYou suddenly spot the monster  you're looking to hunt!\nWhat'll you do?!\nRun or Swing at the  MONSTER?!**\n((Write SWING or RUN, The monster has 50 HP.))");
            swingResponse(getInput.next());     
        } else if(check.equalsIgnoreCase("QUIT")) System.out.println("You've decided to quit the game!");
        else System.out.println("Please ENTER only RANDOMIZE or QUIT.");
            return check;
    }
    Should be written as:

    Java Code:
    public static void randCheck(String check) {
        if(check.equalsIgnoreCase("RANDOMIZE")) {
            System.out.println("Your stats have been randomized!");
            System.out.println("STATS: Health " + randomizeHEALTH() + "\t Attack " + randomizeATTACK());
                 
            System.out.println("**You  began walking throughout the  forest.\nYou suddenly spot the monster  you're looking to hunt!\nWhat'll  you do?!\nRun or Swing at the  MONSTER?!**\n((Write SWING or RUN, The  monster has 50 HP.))");
            swingResponse(getInput.next());     
        } else if(check.equalsIgnoreCase("QUIT")) {
              System.out.println("You've decided to quit the game!");
        } else {
              System.out.println("Please ENTER only RANDOMIZE or QUIT.");
        }
    }
    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Problem: New to this and cannot figure this one out.
    By JohnSalx28 in forum New To Java
    Replies: 2
    Last Post: 08-01-2013, 10:19 PM
  2. Replies: 5
    Last Post: 12-11-2011, 06:42 PM
  3. Replies: 7
    Last Post: 07-17-2010, 12:22 PM
  4. Unable to figure out this problem! jndi lookup exception.....
    By vrk in forum Enterprise JavaBeans (EJB)
    Replies: 0
    Last Post: 01-19-2009, 11:38 AM
  5. [SOLVED] Can't figure out my thread/awt problem
    By Zosden in forum Advanced Java
    Replies: 7
    Last Post: 07-29-2008, 05:13 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •