Hello,
I am fairly new to java, roughly about three to four months in. I am having a problem with my java program. My goal is to request the user to enter how many times they want to roll a pair of dices. Each dice have numbers 1-6. When rolling the dices, I randomly pick a number from each dice and total the number. My goal is to calculate the number of times snake-eyes was rolled which is a total of 2 and total number of times a total of 7 was rolled. Here is my code. I am calling the rollDice method to perform the random pick and calculations. The error I am getting is at the bottom of the code. Any suggestions? Your help will be greatly appreciated.
package dice;
import java.util.Scanner;
import java.util.Random;
public class Dice
{
public static void main(String[] args)
{
int numRolls; // number of rolls
Scanner nord = new Scanner(System.in); // create InputStream
System.out.print("How many times would you like to roll the two dice? "); // prompt to enter how many times to roll dice
numRolls = nord.nextInt(); // variable to store number of rolls
// call rollDice method
rollDice(numRolls);
} //end main
public static void rollDice(int numRolls)
{
int d = numRolls;
int[] diceOne = null; // dice one variable
int[] diceTwo = null; // dice two variable
int crapsRolled = 0; // variable to hold how many times 7 was rolled
int snakeEysRolled = 0; // variable to hold how many times snake eyes was rolled
int totalDiceAmt; // count dice total
int[] diceRolls; // variable to store total number of dice rolls
int[] numArr = {1, 2, 3, 4, 5, 6}; // array to hold numbers on dice
// create an object from the Random class
Random r = new Random();
diceRolls = new int[d]; //create array to hold number of rolls
// loop through number of times you rolled
for (int i = 0; i < diceRolls.length; i++)
{
diceOne[i] = r.nextInt(numArr.length);
diceTwo[i] = r.nextInt(numArr.length);
totalDiceAmt = diceOne[i] + diceTwo[i];
// loop through number of times 2 was rolled
if (totalDiceAmt == 2)
{
snakeEysRolled++;
}
// loop through number of times 7 was rolled
if (totalDiceAmt == 7 )
{
crapsRolled++;
}
}
// print statement
System.out.println("Sneke Eyes was rolled " + snakeEysRolled + " times");
// print blank line
System.out.println();
System.out.println("Craps was rolled " + crapsRolled + " times");
} // end roll dice method
} // end Dice class
run:
How many times would you like to roll the two dice? 1000
Exception in thread "main" java.lang.NullPointerException
at dice.Dice.main(Dice.java:40)
Java Result: 1
BUILD SUCCESSFUL (total time: 4 seconds)