I need to make a dicetester for school. The application shows a graph and the dice of the thrown numbers and puts these numbers into an int array. The Reset button needs to clear this array and update the interface but this doesn't seem to be working.
It gives the following error code: Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
public class DiceModel extends Observable {
private int[] numbers;
private int totalThrows;
private int currentThrow;
private boolean diceEnabled;
private boolean graphView;
/**
* Initialize dice model
*/
public DiceModel() {
numbers = new int[6];
currentThrow = 0;
diceEnabled = false;
graphView = true;
}
//addObserver(view);
public void addObs(DiceView x){
addObserver(x);
}
public void addObs(DiceGraphView x){
addObserver(x);
}
public void addObs(DiceTableView x){
addObserver(x);
}
/**
*
* @return last thrown number
*/
public int getCurrentThrow() {
return currentThrow;
}
/**
* notifyObservers, add number to array
* @param x Number to add
*/
public void addNumber(int x) {
totalThrows++;
currentThrow = x;
//zet het randomnummer in de arraylist
int i = numbers[x];
numbers[x] = ++i;
setChanged();
notifyObservers();
}
public void clearArray(){
numbers = null;
setChanged();
}
/**
*
* @return the most thrown number
*/
public int getMaxNumber() {
int x = 0;
for(int i = 0; i < 6; i++)
if(numbers[i] > x)
x = numbers[i];
return x;
}
/**
*
* @param n
* @return Number of thrown n
*/
public int getNumber(int n) {
if(n >= 0 && n < 6)
return numbers[n];
else
return 0;
}
/**
*
* @return total thrown numbers
*/
public int getTotalThrows() {
return totalThrows;
}
/**
*
* @return average
*/
public int getAverage() {
int x = 0;
for(int i = 0; i < 6; i++)
if(numbers[i] > 0)
x += numbers[i]*(i+1);
return x/totalThrows;
}
/**
*
* @return boolean diceEnabled
*/
public boolean isDiceEnabled() {
return diceEnabled;
}
/**
* Switch diceEnabled and notifyObservers
*/
public void switchDiceEnabled() {
diceEnabled = !diceEnabled;
setChanged();
notifyObservers();
}
/**
*
* @return boolean graphView
*/
public boolean isGraphView() {
return graphView;
}
/**
* Switch frequency table and
*/
public void switchView() {
graphView = !graphView;
setChanged();
notifyObservers();
}
}
public class DiceView extends JPanel implements Observer {
private DiceModel model;
private DiceGraphView graphView;
private DiceTableView tableView;
private boolean isGraphView;
private BorderLayout layout;
private Panel panel;
/**
* Initialize dice view and add graphView
* @param model Add dice model for mvc
*/
DiceView(DiceModel model){
this.model = model;
isGraphView = model.isGraphView();
graphView = new DiceGraphView(model);
tableView = new DiceTableView(model);
model.addObs(graphView);
model.addObs(tableView);
setLayout(new BorderLayout());
panel = new Panel();
layout = new BorderLayout();
panel.setLayout(layout);
panel.add(graphView,BorderLayout.CENTER);
add(panel, BorderLayout.CENTER);
}
/**
* Check if view has changed from model
* @param obs
* @param x
*/
public void update(Observable obs, Object x)
{
if(isGraphView != model.isGraphView()) {
isGraphView = model.isGraphView();
switchView();
}
}
/**
* Removes views from panel and add graph or table view
* Revalidate panel and repaint panel.
*/
public void switchView() {
panel.removeAll();
if(isGraphView) {
panel.add(graphView, BorderLayout.CENTER);
} else {
panel.add(tableView,BorderLayout.CENTER);
}
panel.revalidate();
panel.repaint();
}
}
public class DiceControl extends JPanel implements ChangeListener,ActionListener {
private JButton start;
private JButton stop;
private JButton showDice;
private JButton switchView;
private JButton reset;
private JSlider delay;
private JTextField speed;
private DiceModel model;
private DiceControl control;
private Timer timer;
private Random randomGenerator;
private JLabel info;
/**
* Initialize dice control
* @param model Add dice model for mvc
*/
public DiceControl(DiceModel model)
{
this.model = model;
control = this; // timer needs control
randomGenerator = new Random(); //maak nieuwe random
//maak alle button's etc.
info = new JLabel("Total throws 0, average 0");
start = new JButton("Start");
stop = new JButton("Stop");
reset = new JButton("Reset");
showDice = new JButton("Show / hide dice");
switchView = new JButton("Switch view");
speed = new JTextField("500 ms");
speed.setEditable(false);
speed.setColumns(5);
delay = new JSlider(JSlider.HORIZONTAL, 1, 40,25);
delay.addChangeListener(this);
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(5,5,5,5); //van alle kanten 5 van de kant.
gbc.weightx = 1.0;
gbc.weighty = 1.0;
gbc.gridx = 0;
gbc.gridy = 0;
add(start, gbc);
gbc.gridx++; //x as ++ anders overlappen de buttons.
add(stop, gbc);
gbc.gridx++;
add(reset, gbc);
gbc.gridx++;
add(showDice, gbc);
gbc.gridx++;
add(switchView, gbc);
gbc.gridx = 0;
gbc.gridy++;
gbc.gridwidth = 4;
gbc.fill = GridBagConstraints.HORIZONTAL;
Panel panel = new Panel();
panel.setLayout(new BorderLayout());
panel.add(delay, BorderLayout.CENTER);
panel.add(speed, BorderLayout.EAST);
add(panel, gbc);
gbc.gridy++;
gbc.gridx=3;
gbc.gridwidth=1;
add(info,gbc);
//connect de objecten met de button dmv addActionListener() method.
start.addActionListener(new Start());
stop.addActionListener(new Stop());
reset.addActionListener(new Reset());
showDice.addActionListener(new SwitchDice());
switchView.addActionListener(new SwitchView());
}
/**
* Slicer value has changed
* @param e
*/
public void stateChanged(ChangeEvent e) {
//als de slider value verandert is verander de snelheid weergegeven op het label
speed.setText((delay.getValue()*50)+" ms"); //maal 50 want jslider loopt van 1-40
}
public void update()
{
//update de info
info.setText("Total throws "+model.getTotalThrows()+", average "+model.getAverage());
}
/**
* Timer has triggered event
* add random number to model
* @param event
*/
public void actionPerformed(ActionEvent event)
{
int randomInt = randomGenerator.nextInt(6); //tussen de 0 inc en 6 ex
model.addNumber(randomInt);
//add het nummer in de arraylist in de class DiceModel
update();
System.out.println(randomInt+1);
}
class Start implements ActionListener
{
public void actionPerformed(ActionEvent event) {
if (timer == null || !timer.isRunning())
{
timer = new Timer(delay.getValue() * 50, control);
timer.start();
}
start.setEnabled(false);
stop.setEnabled(true);
}
}
class Stop implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
if(timer != null && timer.isRunning()) {
timer.stop();
}
start.setEnabled(true);
stop.setEnabled(false);
}
}
class Reset implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
timer.stop();
model.clearArray();
start.setEnabled(true);
stop.setEnabled(false);
}
}
class SwitchDice implements ActionListener
{
public void actionPerformed(ActionEvent event){
model.switchDiceEnabled();
}
}
class SwitchView implements ActionListener
{
public void actionPerformed(ActionEvent event) { model.switchView(); }
}
}