I'm making a simple method for a Java game with a class called DealOrNoDeal
.
It's a very simple method to get a int input from user between two bounds. But for some reason it thrown a java.lang.NullPointerException
.
This is my code:
import java.util.Scanner;
import java.util.Arrays;
public class DealOrNoDeal {
public Scanner scanner;
public static void main(String[] args) {
new DealOrNoDeal().run();
}
public void run() {
Scanner scanner = new Scanner(System.in);
int[] values = {1, 20, 100, 1000, 2000, 5000};
System.out.println("Please select the suitcase that you would like to keep (1-6): ");
int keuze = getIntBetweenBounds(1, 6);
}
private int getIntBetweenBounds(int lowerBound, int upperBound) {
int result = scanner.nextInt();
System.out.println("Please select the suitcase that you would like to keep (1-6): ");
while (result < lowerBound || result > upperBound) {
System.out.println("Please select the suitcase that you would like to keep (1-6): ");
result = scanner.nextInt();
}
return result;
}