Question: Hello, I'm getting a logic error in my code here and I'm not sure what is going wrong. The language is Java. I'm supposed to

Hello,

I'm getting a logic error in my code here and I'm not sure what is going wrong. The language is Java. I'm supposed to be rolling a pair of dice 10000 times and recording the amount of times I get a pair of numbers (two 1's, two 2's etc.)

Here is my console output with the given code:

Number of rolls: 10000

Exception in thread "main" java.lang.NullPointerException

at edu.ilstu.DiceSimulationDriver.main(DiceSimulationDriver.java:41)

Here is my code:

DIE CLASS

import java.util.Random;

public class Die

{

private Random dice = new Random();

private int spots;

public Die(Random dice, int spots)

{

this.dice = dice;

this.spots = spots;

}

public void roll(Random dice)

{

spots = dice.nextInt(6) + 1;

}

public int getSpots()

{

return spots;

}

public boolean equals(Die other)

{

if(spots == other.getSpots())

{

return true;

}

return false;

}

}

DICE ACCUMULATOR CLASS

public class DiceAccumulator

{

private int snakeEyes;

private int twos;

private int threes;

private int fours;

private int fives;

private int sixes;

public void addSnakeEyes()

{

snakeEyes++;

}

public void addTwos()

{

twos++;

}

public void addThrees()

{

threes++;

}

public void addFours()

{

fours++;

}

public void addFives()

{

fives++;

}

public void addSixes()

{

sixes++;

}

public int getSnakeEyes()

{

return snakeEyes;

}

public int getTwos()

{

return twos;

}

public int getThrees()

{

return threes;

}

public int getFours()

{

return fours;

}

public int getFives()

{

return fives;

}

public int getSixes()

{

return sixes;

}

}

DICE SIMULATOR CLASS

public class DiceSimulator

{

DiceAccumulator diceA;

private int die1;

private int die2;

public DiceAccumulator runSimulation(int numRolls)

{

die1 = (int) (Math.random() * 6) + 1;

die2 = (int) (Math.random() * 6) + 1;

int spots = 0;

if(spots == '1')

{

diceA.addSnakeEyes();

}

else if(spots == '2')

{

diceA.addTwos();

}

else if(spots == '3')

{

diceA.addThrees();

}

else if(spots == '4')

{

diceA.addFours();

}

else if(spots == '5')

{

diceA.addFives();

}

else if(spots == '6')

{

diceA.addSixes();

}

return diceA;

}

}

DICE SIMULATION DRIVER CLASS

public class DiceSimulationDriver

{

/**

* @param args

*/

public static void main(String[] args)

{

//Creating a Dice Simulator object

DiceSimulator simulator = new DiceSimulator();

//Number of rolls

int numRolls = 10000;

DiceAccumulator accumulator = simulator.runSimulation(numRolls);

System.out.println("Number of rolls: " + numRolls);

System.out.println("Number snake eyes: " + accumulator.getSnakeEyes());

System.out.println("Number twos: " + accumulator.getTwos());

System.out.println("Number threes: " + accumulator.getThrees());

System.out.println("Number fours: " + accumulator.getFours());

System.out.println("Number fives: " + accumulator.getFives());

System.out.println("Number sixes: " + accumulator.getSixes());

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!