Hi!!
I've been working on this assingment for class where I have to design and implement an application that displays two Die objects, a button, and a label. Every time the button is pushed, the following should happen: the two Die objects should be rolled, the face value of the two Die objects should be displayed and the label should display the sum of the two dice. I got so close so far, but now the gui won't even pop up with the driver anymore! Can anyone help!! I am putting my 3 programs below : the DIE CLASS, DIE PANEL and DIE DRIVER.
When I try to run the program I get the following error:
Exception in thread "main" java.lang.NullP ointerException
at DiePanel.<init> (DiePanel.java: 29)
at DieDriver.main( DieDriver.java: 17)
Any advice???
[DIE CLASS]
[CODE=java]//Author: Sue McFarland Metzger
//Purpose: Die Class
import javax.swing.*;
public class Die
{
//constants
final static ImageIcon D1 = new ImageIcon( "d1.gif");
final static ImageIcon D2 = new ImageIcon( "d2.gif");
final static ImageIcon D3 = new ImageIcon( "d3.gif");
final static ImageIcon D4 = new ImageIcon( "d4.gif");
final static ImageIcon D5 = new ImageIcon( "d5.gif");
final static ImageIcon D6 = new ImageIcon( "d6.gif");
final static ImageIcon D7 = new ImageIcon( "dice.gif") ;
final static int DEFAULT_SIDES = 6;
//instance data
private int faceValue;
private int sides;
//the constructor
public Die(int numSides)
{
sides = numSides;
faceValue = -1;
}
//another Constructor
//this is an example of method overloading
public Die()
{
sides = DEFAULT_SIDES;
faceValue = -1;
}
//toString method used to display an object
public String toString()
{
return sides + " sided die";
}
//roll die - a setter method
public int roll()
{
faceValue = (int)(Math.rand om() * sides) + 1;
return faceValue;
}
//getter method
public int getFaceValue()
{
return faceValue;
}
//setter method
public void setSides( int sides )
{
this.sides = sides;
return;
}
//display the die as an Icon
public ImageIcon getIcon( )
{
ImageIcon icon;
switch (faceValue)
{
case 1:
icon = D1;
break;
case 2:
icon = D2;
break;
case 3:
icon = D3;
break;
case 4:
icon = D4;
break;
case 5:
icon = D5;
break;
case 6:
icon = D6;
break;
default:
icon = D7;
}
return icon;
}
}//end of the class[/CODE]
[DIE PANEL]
[CODE=java]//import GUI classes
import javax.swing.*;
import javax.swing.JLa bel;
import java.awt.*;
import java.awt.event. *;
public class DiePanel extends JPanel
{
//instance data
private JLabel lab1, lab2, sum;
private JButton roll1, roll2;
Die d1, d2;
int totalSum;
//constructor
public DiePanel()
{
//set the panel's color
this.setBackgro und( Color.YELLOW );
//instantiate components
lab1 = new JLabel (d1.getIcon ());
lab2 = new JLabel (d2.getIcon ());
sum = new JLabel ("Sum:" + totalSum);
roll1 = new JButton( "Roll" );
roll1.addAction Listener (new ButtonListener ());
roll2 = new JButton ("Roll");
roll2.addAction Listener (new ButtonListener ());
//add components to the panel
this.add (roll1);
this.add (lab1);
this.add (roll2);
this.add (lab2);
this.add (sum);
//set size of the panel
this.setPreferr edSize( new Dimension( 200,200 ) );
}
//supporting methods - inner class (ActionListener s)
public class ButtonListener implements ActionListener
{
public void actionPerformed (ActionEvent event)
{
//Declare Variabes
d1 = new Die ();
d2 = new Die ();
int totalsum;
//Process
d1.roll();
lab1.setIcon (d1.getIcon ());
d2.roll();
lab2.setIcon (d2.getIcon ());
}
}
}[/CODE]
[DIE DRIVER]
[CODE=java]//import GUI class for Frames
import javax.swing.*;
public class DieDriver
{
public static void main( String[] args)
{
//construct a new container - the Frame
JFrame frame = new JFrame("Die Application");
frame.setDefaul tCloseOperation ( JFrame.EXIT_ON_ CLOSE );
//construct the primary panel
DiePanel newPanel = new DiePanel();
//add the primary panel to the frame
frame.add( newPanel );
//compress the frame and make it visible
frame.pack();
frame.setVisibl e( true ); //make frame visible to user
}
}[/CODE]
I've been working on this assingment for class where I have to design and implement an application that displays two Die objects, a button, and a label. Every time the button is pushed, the following should happen: the two Die objects should be rolled, the face value of the two Die objects should be displayed and the label should display the sum of the two dice. I got so close so far, but now the gui won't even pop up with the driver anymore! Can anyone help!! I am putting my 3 programs below : the DIE CLASS, DIE PANEL and DIE DRIVER.
When I try to run the program I get the following error:
Exception in thread "main" java.lang.NullP ointerException
at DiePanel.<init> (DiePanel.java: 29)
at DieDriver.main( DieDriver.java: 17)
Any advice???
[DIE CLASS]
[CODE=java]//Author: Sue McFarland Metzger
//Purpose: Die Class
import javax.swing.*;
public class Die
{
//constants
final static ImageIcon D1 = new ImageIcon( "d1.gif");
final static ImageIcon D2 = new ImageIcon( "d2.gif");
final static ImageIcon D3 = new ImageIcon( "d3.gif");
final static ImageIcon D4 = new ImageIcon( "d4.gif");
final static ImageIcon D5 = new ImageIcon( "d5.gif");
final static ImageIcon D6 = new ImageIcon( "d6.gif");
final static ImageIcon D7 = new ImageIcon( "dice.gif") ;
final static int DEFAULT_SIDES = 6;
//instance data
private int faceValue;
private int sides;
//the constructor
public Die(int numSides)
{
sides = numSides;
faceValue = -1;
}
//another Constructor
//this is an example of method overloading
public Die()
{
sides = DEFAULT_SIDES;
faceValue = -1;
}
//toString method used to display an object
public String toString()
{
return sides + " sided die";
}
//roll die - a setter method
public int roll()
{
faceValue = (int)(Math.rand om() * sides) + 1;
return faceValue;
}
//getter method
public int getFaceValue()
{
return faceValue;
}
//setter method
public void setSides( int sides )
{
this.sides = sides;
return;
}
//display the die as an Icon
public ImageIcon getIcon( )
{
ImageIcon icon;
switch (faceValue)
{
case 1:
icon = D1;
break;
case 2:
icon = D2;
break;
case 3:
icon = D3;
break;
case 4:
icon = D4;
break;
case 5:
icon = D5;
break;
case 6:
icon = D6;
break;
default:
icon = D7;
}
return icon;
}
}//end of the class[/CODE]
[DIE PANEL]
[CODE=java]//import GUI classes
import javax.swing.*;
import javax.swing.JLa bel;
import java.awt.*;
import java.awt.event. *;
public class DiePanel extends JPanel
{
//instance data
private JLabel lab1, lab2, sum;
private JButton roll1, roll2;
Die d1, d2;
int totalSum;
//constructor
public DiePanel()
{
//set the panel's color
this.setBackgro und( Color.YELLOW );
//instantiate components
lab1 = new JLabel (d1.getIcon ());
lab2 = new JLabel (d2.getIcon ());
sum = new JLabel ("Sum:" + totalSum);
roll1 = new JButton( "Roll" );
roll1.addAction Listener (new ButtonListener ());
roll2 = new JButton ("Roll");
roll2.addAction Listener (new ButtonListener ());
//add components to the panel
this.add (roll1);
this.add (lab1);
this.add (roll2);
this.add (lab2);
this.add (sum);
//set size of the panel
this.setPreferr edSize( new Dimension( 200,200 ) );
}
//supporting methods - inner class (ActionListener s)
public class ButtonListener implements ActionListener
{
public void actionPerformed (ActionEvent event)
{
//Declare Variabes
d1 = new Die ();
d2 = new Die ();
int totalsum;
//Process
d1.roll();
lab1.setIcon (d1.getIcon ());
d2.roll();
lab2.setIcon (d2.getIcon ());
}
}
}[/CODE]
[DIE DRIVER]
[CODE=java]//import GUI class for Frames
import javax.swing.*;
public class DieDriver
{
public static void main( String[] args)
{
//construct a new container - the Frame
JFrame frame = new JFrame("Die Application");
frame.setDefaul tCloseOperation ( JFrame.EXIT_ON_ CLOSE );
//construct the primary panel
DiePanel newPanel = new DiePanel();
//add the primary panel to the frame
frame.add( newPanel );
//compress the frame and make it visible
frame.pack();
frame.setVisibl e( true ); //make frame visible to user
}
}[/CODE]
Comment