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 3 of 3

Thread: Exception in thread "main" java.lang.NullPointerException

  1. #1
    Member ziplague's Avatar
    Join Date
    Nov 2011
    Location
    Sweden
    Posts
    33
    My Mood
    Cheerful
    Thanks
    9
    Thanked 1 Time in 1 Post

    Default Exception in thread "main" java.lang.NullPointerException

    Hello everyone, i'm doing some exercises from the book "Head first java, O'Reilly, 2nd Edition", but this guessing game doesn't want to work.
    Please can anyone explain to me what this null pointer exception means?
    (i'm using Eclipse Helios with JRE1.7 and JDK1.7)
    All other programs seem to work just fine.

    The Program is in 3 parts, GameLauncher.java GuessGame.java & Player.java. All three of them are in one package called theGuessingGame.

    package theGuessingGame;
     
    public class GameLauncher {
     
    	public static void main (String[] args){
    		/*
    		 * makes a GuessGame object and tells it to startGame.
    		 */
    		GuessGame game = new GuessGame();
    		game.startGame();
    	}
     
    }

    package theGuessingGame;
     
    public class GuessGame {
    	/*
    	 * instance variables for 3 players.
    	 * GuessGame has 3 instance variables for the three
    	 * Player objects.
    	 */
    	Player p1;
    	Player p2;
    	Player p3;
    	/*
    	 * startGame() method starts here.
    	 */
    	public void startGame(){
    		/*
    		 *Create three Player objects and assign them to the 
    		 * three Player instance variables.
    		 */
    		p1 = new Player();
    		p1 = new Player();
    		p1 = new Player();		
    		/*
    		 * Declare 3 variables to hold the three guesses
    		 * that the players make.
    		 */
    		int guessp1 = 0;
    		int guessp2 = 0;
    		int guessp3 = 0;		
    		/*
    		 * Declare three variable to hold a true or false 
    		 * based on the player's answer
    		 */
    		boolean p1isRight = false;
    		boolean p2isRight = false;
    		boolean p3isRight = false;
    		/*
    		 * make a target number that the players have to guess.
    		 */
    		int targetNumber = (int) (Math.random()*10);
    		System.out.print("\nI'm thinking of a number between 0 and 9...");
     
    		while (true) {
    			System.out.print("\nNumber to guess is: "+ targetNumber);
     
    			/*
    			 * call each palyer's guess() method.
    			 */
    			p1.guess();
    			p2.guess();
    			p3.guess();
    			/*
    			 * get each player's guess by accessing the number variable of
    			 * each player.
    			 */
    			guessp1 = p1.number;
    			System.out.print("\nPlayer one guessed: "+ guessp1);
     
    			guessp2 = p2.number;
    			System.out.print("\nPlayer one guessed: "+ guessp2);
     
    			guessp3 = p3.number;
    			System.out.print("\nPlayer one guessed: "+ guessp3);			
    			/*
    			 * check each player's guess to see if it matches the target
    			 * number. If a player is right, then set that player's variable
    			 * to be true. (we set it false by default)
    			 */
    			if (guessp1 == targetNumber){
    				p1isRight = true;
    			}
    			if (guessp2 == targetNumber){
    				p2isRight = true;
    			}
    			if (guessp3 == targetNumber){
    				p3isRight = true;
    			}
    			/*
    			 * if player one or two or three is right...
    			 */
    			if (p1isRight || p2isRight || p3isRight){
    				System.out.print("\nWe have a winner!!");
    				System.out.print("\nPlayer one is right? "+p1isRight);
    				System.out.print("\nPlayer two is right? "+p2isRight);
    				System.out.print("\nPlayer three is right? "+p3isRight);
    				System.out.print("Game is over!");
    				break; //Game of over so break out of the loop.
    			}	else	{
    				//We must keep going because nobody got it right!
    				/*
    				 * otherwise stay in the loop and ask the player for another guess.
    				 */
    				System.out.print("\nPlayer will ahve to try again!");
    			}//End of if/else
    		}//End of while loop
    	}//End of method
    }//End of class

    package theGuessingGame;
     
    public class Player {
    	/*
    	 * the number this player guessed.
    	 */
    	int number = 0;//where the guess goes.
    	/*
    	 * guess() method for making a guess starts here.
    	 */
    	public void guess(){
    		number = (int) (Math.random()*10);
    		System.out.print("\nI'm guessing "+ number);	
    	}
    }

    The Error looks like this :

    Exception in thread "main" java.lang.NullPointerException
    at theGessingGame.GuessGame.startGame(GuessGame.java: 50)
    at theGessingGame.GameLauncher.main(GameLauncher.java :10)


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Exception in thread "main" java.lang.NullPointerException

    Look closely at your code...something is null - add some println's in there to print out each object and see what is null (hint, take a look at p2 and p3 variables)

  3. The Following User Says Thank You to copeg For This Useful Post:

    ziplague (November 26th, 2011)

  4. #3
    Member ziplague's Avatar
    Join Date
    Nov 2011
    Location
    Sweden
    Posts
    33
    My Mood
    Cheerful
    Thanks
    9
    Thanked 1 Time in 1 Post

    Default Re: Exception in thread "main" java.lang.NullPointerException

    found it )) a copy paste mistake, p2 and p3 were null. Now it's working thx

Similar Threads

  1. Replies: 1
    Last Post: August 31st, 2011, 10:48 AM
  2. Replies: 7
    Last Post: May 30th, 2011, 09:11 AM
  3. Exception in thread "main" java.lang.NullPointerException
    By Tsark in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 11th, 2011, 08:39 AM
  4. Exception in thread "main" java.lang.NullPointerException
    By MryJaho in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 4th, 2011, 05:36 PM
  5. Replies: 16
    Last Post: August 27th, 2010, 03:30 PM

Tags for this Thread