473,480 Members | 1,885 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Stuck on your typical Die Class example with a Gui Interface..HELP!!!

1 New Member
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.NullPointerException
at DiePanel.<init>(DiePanel.java:29)
at DieDriver.main(DieDriver.java:17)

Any advice???








[DIE CLASS]
Expand|Select|Wrap|Line Numbers
  1. //Author: Sue McFarland Metzger
  2. //Purpose: Die Class
  3.  
  4.    import javax.swing.*;
  5.  
  6.     public class Die
  7.    {
  8.    //constants
  9.       final static ImageIcon D1 = new ImageIcon( "d1.gif");
  10.       final static ImageIcon D2 = new ImageIcon( "d2.gif");
  11.       final static ImageIcon D3 = new ImageIcon( "d3.gif");
  12.       final static ImageIcon D4 = new ImageIcon( "d4.gif");
  13.       final static ImageIcon D5 = new ImageIcon( "d5.gif");
  14.       final static ImageIcon D6 = new ImageIcon( "d6.gif");
  15.       final static ImageIcon D7 = new ImageIcon( "dice.gif");
  16.       final static int DEFAULT_SIDES = 6;
  17.  
  18.    //instance data
  19.       private int faceValue;
  20.       private int sides;
  21.  
  22.    //the constructor
  23.        public Die(int numSides)
  24.       {
  25.          sides = numSides;
  26.          faceValue = -1;
  27.       }
  28.  
  29.    //another Constructor
  30.    //this is an example of method overloading
  31.        public Die()
  32.       {
  33.          sides = DEFAULT_SIDES;
  34.          faceValue = -1;
  35.       }
  36.  
  37.    //toString method used to display an object
  38.        public String toString()
  39.       {
  40.          return sides + " sided die";
  41.       }
  42.  
  43.    //roll die - a setter method
  44.        public int roll()
  45.       {
  46.          faceValue = (int)(Math.random() * sides) + 1;      
  47.          return faceValue;
  48.       }
  49.  
  50.    //getter method
  51.        public int getFaceValue()
  52.       {
  53.          return faceValue;
  54.       }
  55.  
  56.    //setter method
  57.        public void setSides( int sides )
  58.       {
  59.          this.sides = sides;
  60.          return;
  61.       }
  62.  
  63.    //display the die as an Icon   
  64.        public ImageIcon getIcon( )
  65.       {
  66.          ImageIcon icon;
  67.  
  68.          switch (faceValue)
  69.          {
  70.             case 1:
  71.                icon = D1;
  72.                break;
  73.             case 2:
  74.                icon = D2;
  75.                break;
  76.             case 3:
  77.                icon = D3;
  78.                break;
  79.  
  80.             case 4:
  81.                icon = D4;
  82.                break;
  83.  
  84.             case 5:
  85.                icon = D5;
  86.                break;
  87.  
  88.             case 6:
  89.                icon = D6;        
  90.                break;
  91.  
  92.             default:
  93.                icon = D7;
  94.          }
  95.          return icon;
  96.       }
  97.  
  98.    }//end of the class







[DIE PANEL]
Expand|Select|Wrap|Line Numbers
  1. //import GUI classes
  2. import javax.swing.*;
  3. import javax.swing.JLabel;
  4. import java.awt.*;
  5. import java.awt.event.*;
  6.  
  7. public class DiePanel extends JPanel
  8. {
  9.     //instance data
  10.     private JLabel lab1, lab2, sum;
  11.     private JButton roll1, roll2;
  12.     Die d1, d2;
  13.     int totalSum;
  14.  
  15.     //constructor
  16.     public DiePanel()
  17.     {
  18.  
  19.         //set the panel's color
  20.         this.setBackground( Color.YELLOW );
  21.  
  22.         //instantiate components
  23.         lab1 = new JLabel (d1.getIcon ());
  24.         lab2 = new JLabel (d2.getIcon ());
  25.         sum = new JLabel ("Sum:" + totalSum);
  26.         roll1 = new JButton( "Roll" );
  27.         roll1.addActionListener (new ButtonListener ());
  28.         roll2 = new JButton ("Roll");
  29.         roll2.addActionListener (new ButtonListener ());            
  30.  
  31.  
  32.         //add components to the panel
  33.       this.add (roll1);
  34.       this.add (lab1);
  35.       this.add (roll2);
  36.       this.add (lab2);
  37.         this.add (sum);
  38.  
  39.         //set size of the panel
  40.         this.setPreferredSize( new Dimension( 200,200 ) );
  41.  
  42.     }
  43.  
  44.     //supporting methods - inner class (ActionListeners)
  45.         public class ButtonListener implements ActionListener
  46.         {
  47.             public void actionPerformed (ActionEvent event)
  48.             {
  49.  
  50.                 //Declare Variabes
  51.                 d1 = new Die ();
  52.                 d2 = new Die ();
  53.                 int totalsum;
  54.  
  55.                 //Process
  56.                 d1.roll();
  57.                 lab1.setIcon (d1.getIcon ());
  58.                 d2.roll();
  59.                 lab2.setIcon (d2.getIcon ());
  60.  
  61.  
  62.             }
  63.         }
  64.  
  65. }












[DIE DRIVER]

Expand|Select|Wrap|Line Numbers
  1. //import GUI class for Frames
  2. import javax.swing.*;
  3.  
  4. public class DieDriver
  5. {
  6.     public static void main( String[] args)
  7.     {
  8.  
  9.         //construct a new container - the Frame
  10.         JFrame frame = new JFrame("Die Application");
  11.         frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
  12.  
  13.         //construct the primary panel 
  14.         DiePanel newPanel = new DiePanel();
  15.  
  16.         //add the primary panel to the frame
  17.         frame.add( newPanel );
  18.  
  19.         //compress the frame and make it visible
  20.         frame.pack();        
  21.         frame.setVisible( true );    //make frame visible to user
  22.  
  23.     }
  24. }
Oct 28 '07 #1
1 2167
r035198x
13,262 MVP
Expand|Select|Wrap|Line Numbers
  1. private JButton roll1;
does not create a JButton.
At this point roll1 is null.
So if you do roll1.setWhatever or roll1.addWhatever, you'll get a nullpointer exception because roll1 is not initialized.
To initialize roll1 you'd use
Expand|Select|Wrap|Line Numbers
  1. roll1 = new JButton("Maybe some text here");
Oct 29 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

9
7540
by: phl | last post by:
hi, I am kind of confused aobut interfaces and abstract classes. In short as I understand it, an interface is like a contract between the class and the interface, so that certain funtions must...
10
2941
by: Brett | last post by:
I'm still trying to figure out concrete reasons to use one over the other. I understand the abstract class can have implementation in its methods and derived classes can only inherit one abstract...
10
667
by: Joe | last post by:
My question is more an OOD question. I know *how* to implement both abstract classes and interfaces. Here's my question - under what circumstacnes does one use an abstract class and under what...
2
1185
by: Sebastian Dau | last post by:
Hey, I'm looking for the right syntax to inherit my class from an Interface and a class at the same time: __interface ITest { };
8
3918
by: Gregory | last post by:
I have a question about using STL containers in C++ class public interface. Lets say that I want to return some container from class method or accept class method parameter as some container. For...
1
2210
by: D Witherspoon | last post by:
Coming up with a scenario here. For example there is the standard .NET MailMessage class. I am creating a project (let's call it CommonBase) that has the following 2 classes ...
9
2119
by: d.adamkiewicz | last post by:
Hello Folks Anybody can show me real world singleton class example? Something that works (is implemented) as part of working solution. Does it make sense to create database handler class that...
3
15546
by: Salman | last post by:
One one please tell me whats the difference between ABSTRACT Class and INTERFACE Class in c++. If you can explain me using examples, that would be more good for me.
4
1588
by: Inam ur Rehman | last post by:
hello Guys I have a nice topic to discuss about, which always asked in every interview which is that............... what is the difference between abstract class and interface, if we do same thing...
10
3124
Dököll
by: Dököll | last post by:
I don't want you nice guys to waste your too much of your time, and confused me in the process. I am hoping you can explain it to me like I am 2 years old, that's it. Hoping to get a handle...
0
7049
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6912
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7052
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
6981
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5348
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4790
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
2989
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1304
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
188
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.