Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 02-07-2008, 03:40 AM
Member
 
Join Date: Feb 2008
Posts: 7
Rep Power: 0
stevemcc is on a distinguished road
Default java.lang.NullPointerException
I am working on a gui application that rolls 5 dice. I have my own separate die and dice classes. I am having trouble making the GUI though. I keep getting a NullPointer.

App.java:
Code:
import javax.swing.JFrame;

public class App {

	public App() {
		// TODO Auto-generated constructor stub
	}

	public static void main(String[] args) {
		JFrame frame = new JFrame ("Push Counter");
	    frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
	    frame.getContentPane().add(new AppPanel());

	    frame.pack();
	    frame.setVisible(true);

	}

}
AppPanel.java:
Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class AppPanel extends JPanel {

	private Die die1, die2, die3, die4, die5 = new Die(6);
	private Dice dice = new Dice();
	private JLabel total, d1, d2, d3, d4, d5 = new JLabel("0");
	private JButton rollAll, roll1, roll2, roll3, roll4, roll5 = new JButton("Roll");
	
	public AppPanel() {
		dice.addDie(die1);
		dice.addDie(die2);
		dice.addDie(die3);
		dice.addDie(die4);
		dice.addDie(die5);
		
		//rollAll.addActionListener(new RollAllListener());
		
		add(rollAll);
		add(total);
		
		add(roll1);
		add(d1);
		
		add(roll2);
		add(d2);
		
		add(roll3);
		add(d3);
		add(roll4);
		add(d4);
		add(roll5);
		add(d5);
		
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}
	
	
	
	
   private class RollAllListener implements ActionListener
   {
      public void actionPerformed (ActionEvent event)
      {
    	  dice.rollAll();
      }
   }

}
Someone else in the class was having the same problem as me, but theres was due to never initializing their buttons and labels. Mine are initialized so I cant figure out why I am getting this error. Can anyone help me out?
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 02-07-2008, 06:48 AM
hardwired's Avatar
Senior Member
 
Join Date: Jul 2007
Posts: 1,577
Rep Power: 4
hardwired is on a distinguished road
Default
Code:
// die1, die2, die3, die4 are declared and not instantiated
// and so are null.
// die5 = new Die(6) — is not null
private Die die1, die2, die3, die4, die5 = new Die(6);
private Dice dice = new Dice();
// total, d1, d2, d3, d4 are all null, not assigned any value.
// d5 = new JLabel("0") — okay, instantiated.
private JLabel total, d1, d2, d3, d4, d5 = new JLabel("0");
// rollAll, roll1, roll2, roll3, roll4 are all null.
// roll5 = new JButton("Roll") is okay.
private JButton rollAll, roll1, roll2, roll3, roll4, roll5 = new JButton("Roll");
To test/find_out you can try something like:
Code:
public AppPanel() {
    System.out.println("die1 = " + die1);
    dice.addDie(die1);
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 02-08-2008, 10:01 AM
Member
 
Join Date: Feb 2008
Posts: 7
Rep Power: 0
stevemcc is on a distinguished road
Default
yeah the syntax of some of my instance variables was wrong. This resulted in only the last variable being initialized.
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
java.lang.NullPointerException ravian New To Java 1 01-13-2008 08:39 PM
ArrayList: Exception in thread "main" java.lang.NullPointerException susan New To Java 1 07-16-2007 07:32 AM
AWT-EventQueue-0 java.lang.NullPointerException susan NetBeans 2 07-16-2007 07:21 AM
java.lang.NullPointerException Felissa Advanced Java 1 07-05-2007 07:02 AM
Error Java.lang.NullPointerException in JBuilder2006 Jack Other IDEs 2 07-02-2007 03:29 AM


All times are GMT +2. The time now is 08:27 AM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org