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)