My GUI code
package question1;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.border.TitledBorder;
public class DieGUI extends JFrame{
private JLabel NumberOfSides;
private JButton Create, RollDie;
private JTextField EnterSides;
private JTextArea History;
private JPanel optionsPan, diePan, HistoryPan, panel;
private Die dc;
private int count = 0;
public DieGUI() {
//Label created here
NumberOfSides = new JLabel("Number of side:");
//Buttons created here
Create = new JButton("Create");
Create.addActionListener(new ListenerObject());
RollDie = new JButton("Roll Current Die");
RollDie.addActionListener(new ListenerObject());
//Text Field for sides created
EnterSides = new JTextField(15);
//Text Area for history created
History = new JTextArea(18,22);
History.setLineWrap(true);
History.isEditable();
History.setBorder(new TitledBorder(null, null, TitledBorder.LEADING, TitledBorder.TOP, null, null) );
//Creating the left biggest panel
optionsPan = new JPanel();
optionsPan.setBorder(BorderFactory.createLineBorder(Color.black));
//Creating smaller left panel
diePan = new JPanel();
diePan.setLayout(new FlowLayout());
diePan.setBorder(new TitledBorder(null, "New Die", TitledBorder.LEADING, TitledBorder.TOP, null, null) );
diePan.add(NumberOfSides);
diePan.add(EnterSides);
diePan.add(Create);
//Creating right side panel
HistoryPan = new JPanel();
HistoryPan.setBorder(new TitledBorder(null, "Roll History", TitledBorder.LEADING, TitledBorder.TOP, null, null) );
HistoryPan.setLayout(new FlowLayout());
HistoryPan.add(History);
//Creating the full panel
panel = new JPanel();
panel.setLayout(new GridLayout(2,1));
panel.setBorder(new TitledBorder(null, "Dice Options", TitledBorder.LEADING, TitledBorder.TOP, null, null) );
panel.add(diePan);
panel.add(RollDie);
setLayout(new GridLayout(1,2));
add(panel);
add(HistoryPan);
//add(new JScrollPane(History));
}
public static void main(String[] args) {
DieGUI gui = new DieGUI();
gui.setVisible(true);
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gui.setSize(650,350);
gui.setTitle("Dice GUI");
gui.setResizable(false);
}
public class ListenerObject implements ActionListener{
public void actionPerformed(ActionEvent event) {
try {
if(event.getSource()== Create) {
count=0;
History.setText("");
String text = EnterSides.getText();
int sides = Integer.parseInt(text);
Die dc = new Die(sides);
History.append("Creating a new die deletes the old one \n \n New Die: "+dc+"\n");
}
else if(event.getSource()==RollDie) {
dc.roll();
int value = dc.getSideUp();
count++;
History.append("Roll # "+count+" => Value: "+value);
}
}
catch(IllegalArgumentException e) {
History.append("Die must be either 4,6,10,20 or 100 sided \n");
}
}
}
}
Die class we were given
package question1;
public class Die {
//data fields
private int numSides;
private int sideUp;
/**
* Unargumented constructor
*/
public Die() {
this(6);
}
/**
* Argumented constructor
* @param numSides number of sides for the Die
*/
public Die(int numSides) {
if(numSides == 4 || numSides == 6 || numSides == 10 || numSides == 20 || numSides == 100)
this.numSides = numSides;
else
throw new IllegalArgumentException("Invalid number of sides.");
roll();
}
/**
* Copy constructor
* @param in a Die to copy
*/
public Die(Die in) {
this.numSides = in.numSides;
roll();
}
/**
* Change the value of side up, depends on the number of sides
*/
public void roll() {
sideUp = (int)(Math.random()*numSides)+1;
}
/**
* Get the side up (showing)
* @return side up
*/
public int getSideUp() { roll(); return sideUp; }
/**
* Get the number of sides
* @return number of sides
*/
public int getNumSides() { return numSides; }
/**
* Standard toString for debugging
*/
public String toString() { return "Die[" + numSides + ", showing: " + sideUp + "]"; }
/**
* Standard equals
*/
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Die other = (Die) obj;
if (numSides != other.numSides)
return false;
if (sideUp != other.sideUp)
return false;
return true;
}
}
Error received
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at question1.DieGUI$ListenerObject.actionPerformed(DieGUI.java:96) at javax.swing.AbstractButton.fireActionPerformed(Unknown Source) at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source) at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source) at javax.swing.DefaultButtonModel.setPressed(Unknown Source) at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source) at java.awt.Component.processMouseEvent(Unknown Source) at javax.swing.JComponent.processMouseEvent(Unknown Source) at java.awt.Component.processEvent(Unknown Source) at java.awt.Container.processEvent(Unknown Source) at java.awt.Component.dispatchEventImpl(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source) at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source) at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Window.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.EventQueue.dispatchEventImpl(Unknown Source) at java.awt.EventQueue.access$500(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue$4.run(Unknown Source) at java.awt.EventQueue$4.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue.dispatchEvent(Unknown Source) at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.run(Unknown Source)