Results 1 to 8 of 8
Thread: simple GUI, actionListener
- 12-02-2012, 06:09 PM #1
Member
- Join Date
- Aug 2012
- Posts
- 9
- Rep Power
- 0
simple GUI, actionListener
I'm testing swing, and I have error after I click on button
If, I change line 34 to something else it's working, but that method should change label of button (przycisk).Java Code:Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at mojaGUI.actionPerformed(mojaGUI.java:35)
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"); } }
-
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.
- 12-02-2012, 07:14 PM #3
Member
- Join Date
- Aug 2012
- Posts
- 9
- Rep Power
- 0
Re: simple GUI, actionListener
wow, I totally missed it. Thanks a lot ;)
-
Re: simple GUI, actionListener
- 12-02-2012, 07:18 PM #5
Member
- Join Date
- Aug 2012
- Posts
- 9
- Rep Power
- 0
Re: simple GUI, actionListener
but how? I'm using eclipse.
-
Re: simple GUI, actionListener
Eclipse Menus:
window - preferences - Java - Compiler - Errors/Warnings
Then scroll down to Name shadowing and conflicts
- 12-02-2012, 08:37 PM #7
Member
- Join Date
- Nov 2012
- Posts
- 82
- Rep Power
- 0
Re: simple GUI, actionListener
Hello, since I am new to java too, I`d add an advice for events:
Always do this:
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.Java Code:public void actionPerformed(ActionEvent e) { screen += ((JButton)e.getSource()).getText(); textFields.get("Screen1").setText(screen); System.out.println("dbg#"+screen); }
-
Re: simple GUI, actionListener
Similar Threads
-
Actionlistener q
By stuckonjava in forum New To JavaReplies: 2Last Post: 05-16-2012, 08:17 PM -
ActionListener Help
By rakosky in forum AWT / SwingReplies: 4Last Post: 04-06-2012, 03:59 PM -
Please help with actionlistener
By ADustedEwok in forum New To JavaReplies: 5Last Post: 12-08-2011, 10:04 PM -
ActionListener
By jaylimix in forum Java AppletsReplies: 5Last Post: 11-06-2011, 06:05 PM -
Please Help With ActionListener
By Daman12 in forum New To JavaReplies: 29Last Post: 10-26-2011, 07:43 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks