Results 1 to 4 of 4
Like Tree1Likes
  • 1 Post By Fubarable

Thread: Null Pointer Exception in a very Simple Action Listener. Why ?

  1. #1
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    4

    Default Null Pointer Exception in a very Simple Action Listener. Why ?

    I have a MainFrame class that extends JFrame and constructs itself and adds a Jbutton called rollDice.

    Here is my Test Class:

    Java Code:
    public class MyApplication 
    {
    	private MainFrame mainFrame;
    	private Brain brain;
    	
    	MyApplication()
    	{
    		this.mainFrame = new MainFrame();
    		this.mainFrame.rollDice.addActionListener(new RollDiceListener());
    	}
    	
    	class RollDiceListener implements ActionListener
    	{
    		@Override
    		public void actionPerformed(ActionEvent e) 
    		{
    			System.out.println(brain.rollDie());
    			mainFrame.validate();
    		}
    	}
    }
    and my Brain Class:

    Java Code:
    public class Brain {
    	
    	Random randomNumberGenerator;
    	
    	public int rollDie()
    	{
    		randomNumberGenerator = new Random();
    		return (randomNumberGenerator.nextInt(6))+1;
    	}
    
    }
    When I click the Roll Dice button I get:

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at myPaackkage.MyApplication$RollDiceListener.actionP erformed(MyApplication.java:24)
    at javax.swing.AbstractButton.fireActionPerformed(Unk nown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed (Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed (Unknown Source)

    Any help ?

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default Re: Null Pointer Exception in a very Simple Action Listener. Why ?

    You need to create an instance of Brain in the MyApplication class.
    Java Code:
    private Brain brain;
    ...brain is never initialized.

  3. #3
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    4

    Default Re: Null Pointer Exception in a very Simple Action Listener. Why ?

    Thanks.. So simple..

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Null Pointer Exception in a very Simple Action Listener. Why ?

    The key learning point is not to simply fix the NPE, but to understand that if you study the variables on the line the NPE is thrown, you often can back-track to find out why one of them is null. Also, learn to use System.out.println(...) debugging statements.
    DarrylBurke likes this.

Similar Threads

  1. null pointer exception help
    By captain_turkiye in forum New To Java
    Replies: 17
    Last Post: 12-04-2011, 01:27 AM
  2. null pointer exception
    By Herah in forum New To Java
    Replies: 1
    Last Post: 12-01-2011, 09:44 AM
  3. Null pointer exception a
    By TaxpayersMoney in forum New To Java
    Replies: 5
    Last Post: 08-16-2011, 01:37 AM
  4. Null Pointer Exception HELP!?
    By 2wyked in forum New To Java
    Replies: 3
    Last Post: 04-04-2011, 02:41 AM
  5. Simple Action/Listener Help GUI
    By aanders5 in forum New To Java
    Replies: 24
    Last Post: 10-18-2010, 07:43 PM

Posting Permissions

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