Results 1 to 8 of 8
  1. #1
    musclecode is offline Member
    Join Date
    Aug 2012
    Posts
    9
    Rep Power
    0

    Default simple GUI, actionListener

    I'm testing swing, and I have error after I click on button

    Java Code:
    Exception in thread "AWT-EventQueue-0"
    java.lang.NullPointerException
    	at mojaGUI.actionPerformed(mojaGUI.java:35)
    If, I change line 34 to something else it's working, but that method should change label of button (przycisk).

    Java Code:
    import java.awt.event.*;
    import javax.swing.*;
    
    
    public class mojaGUI implements ActionListener {
    
    	JButton przycisk;
    	
    	public static void main(String[] args) {
    		
    		mojaGUI okno = new mojaGUI();
    		okno.rob();
    	}
    	
    		public void rob()
    		{
    			JFrame ramka = new JFrame();
    			JButton przycisk = new JButton("potwierdz");
    			
    			przycisk.addActionListener(this);
    			
    			ramka.getContentPane().add(przycisk);
    			ramka.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    			ramka.setSize(400,400);
    			ramka.setVisible(true);
    			
    			
    		}
    		
    
    	
    	public void actionPerformed(ActionEvent event)
    	{
    		przycisk.setText("asdf");
    	}
    
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: simple GUI, actionListener

    You have a classic case of variable shadowing. Since you have re-declared the przycisk variable in the class's constructor and have only initialized the przycisk that is local to the constructor, the class przycisk variable remains null. Solution: don't re-declare the variable in the constructor. Initialize and use the class przycisk variable instead.

  3. #3
    musclecode is offline Member
    Join Date
    Aug 2012
    Posts
    9
    Rep Power
    0

    Default Re: simple GUI, actionListener

    wow, I totally missed it. Thanks a lot ;)

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

    Default Re: simple GUI, actionListener

    Quote Originally Posted by musclecode View Post
    wow, I totally missed it. Thanks a lot ;)
    You're welcome. It's something that we all do, believe me. If you're using an IDE, you can often set it to flag you for these sorts of problems. I know that Eclipse has this ability.

  5. #5
    musclecode is offline Member
    Join Date
    Aug 2012
    Posts
    9
    Rep Power
    0

    Default Re: simple GUI, actionListener

    but how? I'm using eclipse.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: simple GUI, actionListener

    Eclipse Menus:
    window - preferences - Java - Compiler - Errors/Warnings

    Then scroll down to Name shadowing and conflicts

  7. #7
    heatblazer is offline Senior Member
    Join Date
    Nov 2012
    Posts
    131
    Rep Power
    0

    Default Re: simple GUI, actionListener

    Hello, since I am new to java too, I`d add an advice for events:
    Always do this:

    Java Code:
     public void actionPerformed(ActionEvent e) {
    		
    			screen += ((JButton)e.getSource()).getText();
    			textFields.get("Screen1").setText(screen);
    			System.out.println("dbg#"+screen);
    	}
    the line with e.getSource() will return the object that fired the event, thus getText(), in my case it`s the label of the buttons, will add the number to the screen of the calculator-like GUI, I`ve made. It was hard to me to understand for 2-3 hours today, since I am learning events self educationary.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: simple GUI, actionListener

    Quote Originally Posted by heatblazer View Post
    Hello, since I am new to java too, I`d add an advice for events:
    Always do this:

    Java Code:
     public void actionPerformed(ActionEvent e) {
    		
    			screen += ((JButton)e.getSource()).getText();
    			textFields.get("Screen1").setText(screen);
    			System.out.println("dbg#"+screen);
    	}
    the line with e.getSource() will return the object that fired the event, thus getText(), in my case it`s the label of the buttons, will add the number to the screen of the calculator-like GUI, I`ve made. It was hard to me to understand for 2-3 hours today, since I am learning events self educationary.
    I'm not sure how this advice applies to the original poster's code and problem.

Similar Threads

  1. Actionlistener q
    By stuckonjava in forum New To Java
    Replies: 2
    Last Post: 05-16-2012, 08:17 PM
  2. ActionListener Help
    By rakosky in forum AWT / Swing
    Replies: 4
    Last Post: 04-06-2012, 03:59 PM
  3. Please help with actionlistener
    By ADustedEwok in forum New To Java
    Replies: 5
    Last Post: 12-08-2011, 10:04 PM
  4. ActionListener
    By jaylimix in forum Java Applets
    Replies: 5
    Last Post: 11-06-2011, 06:05 PM
  5. Please Help With ActionListener
    By Daman12 in forum New To Java
    Replies: 29
    Last Post: 10-26-2011, 07:43 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
  •