Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 02-07-2008, 03:40 AM
Member
 
Join Date: Feb 2008
Posts: 7
stevemcc is on a distinguished road
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
Sponsored Links
  #2 (permalink)  
Old 02-07-2008, 06:48 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,124
hardwired is on a distinguished road
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
stevemcc is on a distinguished road
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
Sponsored Links
Reply


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

vB 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 +3. The time now is 12:04 AM.


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