0

This program is just an explanation program from a java book . However after I compiled and ran the program I got a run time error. I am new to programming in java. The code is as follows:

class GuessGame{
  Player p1;
  Player p2;
  Player p3;
  public void startgame(){
    p1=new Player();    
    p1=new Player();
    p1=new Player();
    int guessp1=0;
    int guessp2=0;
    int guessp3=0;
    boolean p1isRight=false;
    boolean p2isRight=false;
    boolean p3isRight=false;
    int targetNumber= (int) (Math.random() * 10);
    System.out.println("Ï am thinking of a number between 0 and 9");
    while(true){
    p1.guess();
    p2.guess();
    p3.guess();
    guessp1=p1.number;
    System.out.println("Player 1 guessed " +guessp1);
    guessp2=p2.number;
    System.out.println("Player 2 guessed " +guessp2);
    guessp3=p3.number;
    System.out.println("Player 3 guessed " +guessp3);
    if(guessp1==targetNumber)
      p1isRight=true;
    if(guessp2==targetNumber)
      p2isRight=true;
    if(guessp3==targetNumber)
      p3isRight=true;
    if(p1isRight || p2isRight || p3isRight){
        System.out.println("We have a winner!!");
        System.out.println("Player 1 got it right ?" +p1isRight);
        System.out.println("Player 2 got it right ?" +p2isRight);
        System.out.println("Player 3 got it right ?" +p3isRight);
        System.out.println("Game Over");
        break;
    }
    else{
        System.out.println("Players will have to try again");
    }
    }
  }
}

class Player{
    int number=0;
    public void guess(){
        number= (int) (Math.random() * 10); 
        System.out.println("Ï am guessing " +number);
    }
}

public class GameLauncher{
  public static void main(String [] args){
    GuessGame game=new GuessGame();
    game.startgame();
  }
}

The error i get is :

? am thinking of a number between 0 and 9
? am guessing 0
Exception in thread "main" java.lang.NullPointerException
        at GuessGame.startgame(GameLauncher.java:19)
        at GameLauncher.main(GameLauncher.java:59)
JANNU
  • 13
  • 2
  • 1
    possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – Erwin Bolwidt Jan 21 '15 at 06:30
  • 1
    @JANNU You should probably learn to use a debugger. – Biffen Jan 21 '15 at 06:33

3 Answers3

3

The first three lines of startgame() are

p1=new Player();    
p1=new Player();
p1=new Player();

but should be

p1=new Player();
p2=new Player();
p3=new Player();

Otherwise p2 (and later p3) is null when you try and access it.

Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • Ah typed it by mistake. But I did learn why I got the NullPointerException, so that's good . The other problem I found is that the System.out.println command prints "?" instead of "I". – JANNU Jan 21 '15 at 06:47
1

p2 and p3 are never intialized. Must be a silly mistake, you have made.

Please intialize the variables.

p1=new Player();    
p2=new Player();
p3=new Player();
Ankur Singhal
  • 26,012
  • 16
  • 82
  • 116
1

You did mistake while initiating object

        p1=new Player();    
        p1=new Player();
        p1=new Player();

Corrected code is:

        p1=new Player();    
        p2=new Player();
        p3=new Player();
Kromster
  • 7,181
  • 7
  • 63
  • 111
amit kate
  • 120
  • 3