My code compiles and seemingly runs fine until I hit the JButton and then I get a lot of errors. I'm not sure what I'm doing wrong. What the code is supposed to do is have a window come up and randomly display two image of dice when the button is clicked. The images are in the same directory as my program and they are named 1-6.
Here's the code:
import java.lang.Math;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class DiceSimulator extends JFrame
{
private JLabel dieOne;
private JLabel dieTwo;
public DiceSimulator()
{
setTitle("Dice Simulator");
JLabel dieOne, dieTwo;
dieOne = new JLabel();
dieTwo = new JLabel();
JButton button = new JButton("Roll the Dice");
button.addActionListener(new buttonListener());
setLayout(new BorderLayout());
JPanel panel = new JPanel();
panel.add(button, BorderLayout.SOUTH);
panel.add(dieOne);
panel.add(dieTwo);
add(panel);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setVisible(true);
}
private class buttonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
ImageIcon one = new ImageIcon("1.jpg");
ImageIcon two = new ImageIcon("2.jpg");
ImageIcon three = new ImageIcon("3.jpg");
ImageIcon four = new ImageIcon("4.jpg");
ImageIcon five = new ImageIcon("5.jpg");
ImageIcon six = new ImageIcon("6.jpg");
int firstRoll = (int)(Math.random()*6)+1;
int secondRoll = (int)(Math.random()*6)+1;
switch(firstRoll)
{
case 1: dieOne.setIcon(one);
dieOne.setText(null);
break;
case 2: dieOne.setIcon(two);
dieOne.setText(null);
break;
case 3: dieOne.setIcon(three);
dieOne.setText(null);
break;
case 4: dieOne.setIcon(four);
dieOne.setText(null);
break;
case 5: dieOne.setIcon(five);
dieOne.setText(null);
break;
case 6: dieOne.setIcon(six);
dieOne.setText(null);
break;
}
switch(secondRoll)
{
case 1: dieTwo.setIcon(one);
dieTwo.setText(null);
break;
case 2: dieTwo.setIcon(two);
dieTwo.setText(null);
break;
case 3: dieTwo.setIcon(three);
dieTwo.setText(null);
break;
case 4: dieTwo.setIcon(four);
dieTwo.setText(null);
break;
case 5: dieTwo.setIcon(five);
dieTwo.setText(null);
break;
case 6: dieTwo.setIcon(six);
dieTwo.setText(null);
break;
}
}
}
public static void main(String[] args)
{
new DiceSimulator();
}
}
Here's the errors I get by clicking the button:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at DiceSimulator$buttonListener.actionPerformed(DiceSimulator.java:57)
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$200(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$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.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$1.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)