Results 1 to 3 of 3
  1. #1
    stevemcc is offline Member
    Join Date
    Feb 2008
    Posts
    7
    Rep Power
    0

    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:
    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:
    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?

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java 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:
    Java Code:
    public AppPanel() {
        System.out.println("die1 = " + die1);
        dice.addDie(die1);

  3. #3
    stevemcc is offline Member
    Join Date
    Feb 2008
    Posts
    7
    Rep Power
    0

    Default

    yeah the syntax of some of my instance variables was wrong. This resulted in only the last variable being initialized.

Similar Threads

  1. java.lang.NullPointerException
    By ravian in forum New To Java
    Replies: 1
    Last Post: 01-13-2008, 08:39 PM
  2. Replies: 1
    Last Post: 07-16-2007, 07:32 AM
  3. Replies: 2
    Last Post: 07-16-2007, 07:21 AM
  4. java.lang.NullPointerException
    By Felissa in forum Advanced Java
    Replies: 1
    Last Post: 07-05-2007, 07:02 AM
  5. Replies: 2
    Last Post: 07-02-2007, 03:29 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •