0

Sorry if I am asking this question that has been already asked, but I am kind of a noob at programming, I tried researching how to fix this bug but I am still having trouble trying to solve it. I am trying to compile this block of code here:

//Default constructor
public Game () {
    potAmount = 100;
    betAmount = 0;
}

public int getBetFromUser() {
    //Introduction to the game
    System.out.println("Welcome to Solitaire Dice Game..bet an amount\r\n" + 
            "\t-if you roll triples you win triple your bet,\r\n" + 
            "\t-if you roll doubles you win double your bet,\r\n" + 
            "\t-if you roll 10 or over, you keep your bet\r\n" + 
            "\t-otherwise you lose your bet\r\n" + 
            "A bet of 0 ends the game\r\n");
    System.out.println("Your current pot is 100");
    System.out.println("Enter your bet amount:");
    betAmount = keyboard.nextInt();

    return betAmount;

And I am trying to call it in my main class but I get this compile error:

Enter your bet amount:

Exception in thread "main" java.lang.NullPointerException at Game.getBetFromUser(Game.java:26) at Assign3.main(Assign3.java:9)

JimmyJeans
  • 1
  • 1
  • 2

2 Answers2

2

This is not a compile error, this is exception thrown in runtime. You need to initialize keyboard variable before calling keybord.nextLine()

Ivan
  • 8,508
  • 2
  • 19
  • 30
0

May be you know this thing. But for others I would re-iterate. There is difference between compile time error and run time error. When we compile it, what java compiler does is to check for syntax errors and if no syntax error is present then it would create a .class file out of it. If any syntactical error is there then the code will not compile. Whereas in runtime/execution errors, is an event or situation, which occurs during the execution of a program, that disrupts the normal flow of the program's instructions.

CodeRider
  • 564
  • 4
  • 15