Results 1 to 5 of 5
Thread: Probably doing something stupid
- 04-14-2012, 05:29 PM #1
Member
- Join Date
- Apr 2012
- Posts
- 7
- Rep Power
- 0
Probably doing something stupid
I'm new to swing, having made the change from Netbeans to Eclipse recently. Up until now I had relied on Netbeans for the creation of everything GUI related, but am trying to write it by hand in eclipse now. I've so far created 2 text fields and a button. When I click the button, I'd like for the text in field 1 to be copied to field 2. However, it throws a:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Pizza.MyPanel.actionPerformed(MyPanel.java:46)
Note: The text fields are named textPane as they started off life as textPanes, but I changed to fields to see if it made it work. I'm probably just doing something stupid, so any help would be appreciated.
Edit: I've imported stuff the necassary packages alsopublic class MyPanel extends JPanel implements ActionListener{
private JButton button;
private String name;
private JTextField textPane;
private JTextField textPane2;
public MyPanel() {
JTextField textPane = new JTextField();
textPane.setText("Value");
add(textPane);
button=new JButton("Submit");
button.setVerticalTextPosition(AbstractButton.CENT ER);
button.setHorizontalTextPosition(AbstractButton.LE ADING);
button.setToolTipText("Click to submit");
button.addActionListener(this);
add(button);
JTextField textPane2 = new JTextField();
textPane2.setText("Value");
add(textPane2);
}
public void actionPerformed (ActionEvent e) {
System.out.println("clicked");
name=textPane.getText();
if (name != null){
textPane2.setText(name+" ");
}
else{
System.out.println("null");
}
}
}
- 04-14-2012, 05:34 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,401
- Blog Entries
- 7
- Rep Power
- 17
Re: Probably doing something stupid
Your local variables textPane and textPane2 have nothing to do with your member variables with the same name; your member variables are still equal to null after your constructor has finished; the cure is simple: don't use local variables in your constructor.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 04-14-2012, 06:28 PM #3
Member
- Join Date
- Apr 2012
- Posts
- 7
- Rep Power
- 0
Re: Probably doing something stupid
Ah, I see why it's having an error now, thanks. How do I fix it though? ^^"
- 04-14-2012, 06:34 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,401
- Blog Entries
- 7
- Rep Power
- 17
Re: Probably doing something stupid
As I wrote before: don't use a local variable with the same name as a member variable in your constructor; the first part of your constructor might look like this:
kind regards,Java Code:public MyPanel() { textPane = new JTextField(); // <--- see? textPane.setText("Value"); add(textPane); ...
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 04-14-2012, 07:10 PM #5
Member
- Join Date
- Apr 2012
- Posts
- 7
- Rep Power
- 0
Similar Threads
-
Stupid Array are Stupid.
By Army in forum New To JavaReplies: 10Last Post: 04-13-2012, 06:51 PM -
Stupid mistake, maybe?
By knox in forum New To JavaReplies: 1Last Post: 11-10-2011, 08:39 AM -
How to reverse something really stupid I did... HELP PLEASE
By efebatistaarda in forum NetBeansReplies: 0Last Post: 02-15-2011, 11:16 PM -
I need an idea with something really stupid
By blf_titi in forum Threads and SynchronizationReplies: 2Last Post: 10-27-2010, 03:24 AM -
Stupid error
By dewitrydan in forum Java AppletsReplies: 3Last Post: 08-09-2010, 01:29 PM


2Likes
LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks