-1

I have a program I need to write for a project that has been giving me a boat load of trouble. I'm writing a guessing game that allows you to try to guess a number and for you to give a number and the computer will try to guess it. My major problem is getting the computer to guess and when I try to make it guess it gives me a null pointer exception and I can't figure out how. I know it's a lot but I would be extremely grateful for any help. This is my program... (I will put ///// by the place where I get the null pointer exception)

import java.util.Scanner;
public class NumberGuessClient {
public static void main(String[]args)    {

  Scanner kb = new Scanner (System.in);
  Die number1 = new Die(99);
  int guess;
  String highLow;

System.out.println("What is your name?");
HumanPlayer player1 = new HumanPlayer(kb.next(), 0, 0, 100, 0, 0);
ComputerPlayer computer = new ComputerPlayer(100, "Computer", 0, 0, 0, 0, 100, 1);
System.out.println(player1.toString());

System.out.println("How many points would you like to wager?");
player1.setWager(kb.nextInt());
while(player1.getWager() > player1.getBalance())
{
  System.out.println("You don't have that many points to wager.");
  System.out.println("How many points would you like to wager?");
  player1.setWager(kb.nextInt());
}

//computer chooses number, human player guesses

  int num = number1.roll();
  player1.setGuess(-5);
  int guesses = 0;

  while (player1.getGuess() != num)
  {         
     System.out.println("Enter your guess.");
     player1.setGuess(kb.nextInt());
     if(player1.getGuess() < num)
     {
        System.out.println("Guess is too low!");
        guesses++;
     }

     else
     {
        if(player1.getGuess() > num)
           System.out.println("Guess is too high!");
           guesses++;
     }
  }
  System.out.println("Congratulations!!! You took " + guesses + " turns to guess correctly.");
  if(guesses >= 10){
  System.out.println("You took to many guesses. You lose.");
  player1.lose();
  System.out.println(player1.toString());
  }
  else{
  System.out.println("You win!");
  player1.win();
  System.out.println(player1.toString());
  }

  System.out.println("Computer's turn to guess...");

//human chooses number, computer guesses

  System.out.println(computer.toString());

  System.out.println(computer.getName() + " wagers " + computer.getWager() + " points");
  System.out.println("What is the number the computer needs to guess?");
  num = kb.nextInt();
/////guess = computer.guess();

   guesses = 0;

  while(guess != num){
     guess = computer.guess();
     System.out.println(computer.getName() + " guessed " + guess + ".");
     System.out.println("Is this higher, lower, or at your number? higher/lower/at");
     highLow = kb.next();
     if(highLow.equals("higher"))
     {
        if(guess < computer.getHigh()){
           computer.setHigh(guess);
        }
     }

     if(highLow.equals("lower"))
     {
        if(guess > computer.getLow()){
           computer.setLow(guess);
        }
     }
     guesses++;
  }  

  System.out.println("The computer guessed your number in " + guesses + " tries.");
  if(guesses <= 10){
     System.out.println("It won!");
     computer.win();
  }

  if(guesses > 10){
     System.out.println("It took too many guesses. It lost.");
     computer.lose();
  }
  computer.toString(); 
  }
  }

And here is the method for the computer...

/* This is the class for a computer player for the guessing game
 * A Computer Player has a name, balance, wager, and tracks its wins and losses.
*/
import java.util.Random;

public class ComputerPlayer {

private static Random rand; 
private int balance;
private String name;
private int wager;
private int wins;
private int losses;
private int guess = 1;
private int high;
private int low;

public ComputerPlayer(int bl, String nm, int wr, int ws, int ls, int gs, int hg, int lw) {
  balance = bl;
  name = nm;
  wr = wager;
  ws = wins; 
  gs = guess;
  ls = losses;
  hg = high;
  lw = low;
}


//////public int guess(){   
//////return rand.nextInt(high - low) + low;
}

public void win() {
   wins ++;
}

public void lose() {
   losses ++;
}

public double getBalance() {
   return balance;
}

public int getWager() {
   return 50;
}

public String getName() {
   return name;
}

public int getLosses() {
   return losses;
}

public int getWins() {
   return wins;
}

public void setHigh(int x)
{
   high = x;
}

public void setLow(int x)
{
   low = x;
}

public int getHigh()
{
   return high;
}

public int getLow()
{
   return low;
}




@Override
public String toString() {
   return "Player{" + "name=" + name + ", balance=" + balance + ", wins=" + wins + ", losses="  +  losses + '}';
 }

public void setBalance(int bal) {
   balance = balance;
}
}
  • possible duplicate of [What is a Null Pointer Exception?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception) – Brian Roach Feb 19 '14 at 22:19

1 Answers1

4

In ComputerPlayer.java:

private static Random rand;

You never set this field. Java fields are null if you don't set them to an object instance; null is a value that can be applied to any field referring to an Object, including Random. When you try to refer to a method or field of a null, Java throws a NullPointerException. Try:

private static Random rand = new Random();

If you want a more exact definition of null, try this StackOverflow answer. (Warning: It does get a bit techncial.)

Community
  • 1
  • 1
Jeff Bowman
  • 90,959
  • 16
  • 217
  • 251
  • Thanks so much! Um but now I getthis – user3330119 Feb 19 '14 at 22:36
  • Exception in thread "main" java.lang.IllegalArgumentException: n must be positive at java.util.Random.nextInt(Random.java:300) at ComputerPlayer.guess(ComputerPlayer.java:31) at NumberGuessClient.main(NumberGuessClient.java:65 Sorry, I've never used this site before. – user3330119 Feb 19 '14 at 22:37
  • @user3330119 I think that counts as a separate question. :) Remember, StackOverflow is also designed to make a searchable archive of answers and explanations for people having the same technical problems you do, and learning to debug your code is a good thing that will serve you well. That said, according to the docs, `n` is the argument to `nextInt`, and unless you set `high` and `low` they will both start out as 0. Good luck! – Jeff Bowman Feb 19 '14 at 22:45