0

Hey guys I am sure there is something obvious here that im just not seeing. I need some help with trying to figure out why I am getting a null pointer exception when I set the game.max value in the GuessingGame3 constructor. It seems like no matter what I do the value will not get set. Even when I explicitly set the value it still throws an error. I've tried changing the max variable to static as well and that didnt work either. A lot of people are saying you need to initialize the value which I did and that didnt work. Please help.

   package guessinggame3;
   import java.util.Random;    //provides methods that return a random integer in 
                            //the range 0 to 4294967296.
   import java.util.Scanner;   //enable the user to input values from the keyboard

   public class GuessingGame3 
   {
       public int max;
       public int answer;
       public int maxGuessesAllowed;
       public int numGuessTaken;
       public static int currentGuess;
       public static int previousGuess;
       public boolean gameOver;
       public int differential;
       public GuessingGame3 game;

public static void main(String[] args)
{ 
    GuessingGame3 game = new GuessingGame3();
    game.GuessingGame3();
}

public void GuessingGame3()
{
    Scanner sc = new Scanner(System.in);
    System.out.println("Welcome to the Guessing Game.");
    System.out.println("Enter the maximum number:"); 
    game.max = sc.nextInt();              // input maximun number of guesses
    Random generator;      // New random number generator
    generator = new Random();
    game.answer = generator.nextInt(game.max); // the randomly chosen answer
    System.out.println("Enter the maximum number of guesses allowed:"); 
    maxGuessesAllowed = sc.nextInt(); 
    game.newGame(maxGuessesAllowed);
}

public void newGame(int GuessesAllowed) // Given the max,generate answer
{
 game.maxGuessesAllowed = GuessesAllowed;
 Random generator = new Random();     // New random number generator
 int answer = generator.nextInt(max); // the randomly chosen answer
 System.out.println("The answer is:" + answer);
 game.gameOver = false;
 game.differential = game.max;
 game.numGuessTaken = 0;
 game.guess();
}  

public void guess() 
{ 
 /* Takes in int answer & returns string response */           
 Scanner sc = new Scanner(System.in);
 while(!gameOver)
 {
  System.out.println("Enter your guess:"); 
  currentGuess = sc.nextInt();
  if(answer == currentGuess)     // correct guess branch
   {
    gameOver = true;
    System.out.println("Congratulations! Would you like to play again?");
    System.out.println("Enter Y for yes or N for no.");
    String again = sc.next();  

      again = again.toUpperCase();
      if(again.equals("Y"))
      {
       GuessingGame3 game;
       game = new GuessingGame3();
       game.GuessingGame3(); //Start a new game
      }
      if(again.equals("N"))
      {
       System.out.println("Goodbye.");
       break;
      }
   }   
     if(currentGuess > answer)      //currentGuess is too high
     {
      System.out.println("Too high");
     }
     if(currentGuess <answer)       //currentGuess is too low
     {
      System.out.println("Too low");
     }
     if(currentGuess > max)         //currentGuess is beyond the max
     {
      System.out.print("Your guess is beyond the max you set.");
      continue;
     }
     int differential = Math.abs(answer - currentGuess);
     if (game.differential != 0 && differential < game.differential)   
     {                                       // If this is the first guess         
      System.out.println("Getting Warmer");  // there is differential. So no
     }                                       // "warmer" or "colder."
     if (game.differential != 0 && differential > game.differential)
     {
      System.out.println("Getting Colder");   
     }
     game.differential = differential;
 }
}

}

user3756741
  • 89
  • 1
  • 2
  • 9

0 Answers0