Results 1 to 5 of 5
Like Tree2Likes
  • 1 Post By JosAH
  • 1 Post By JosAH

Thread: Probably doing something stupid

  1. #1
    Vicness is offline Member
    Join Date
    Apr 2012
    Posts
    7
    Rep Power
    0

    Default 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.


    public 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");
    }





    }
    }
    Edit: I've imported stuff the necassary packages also

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,453
    Blog Entries
    7
    Rep Power
    20

    Default 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,

    Jos
    Vicness likes this.
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Vicness is offline Member
    Join Date
    Apr 2012
    Posts
    7
    Rep Power
    0

    Default Re: Probably doing something stupid

    Ah, I see why it's having an error now, thanks. How do I fix it though? ^^"

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,453
    Blog Entries
    7
    Rep Power
    20

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

    Java Code:
    public MyPanel() {
    
       textPane = new JTextField(); // <--- see?
       textPane.setText("Value");
       add(textPane);
       ...
    kind regards,

    Jos
    Vicness likes this.
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    Vicness is offline Member
    Join Date
    Apr 2012
    Posts
    7
    Rep Power
    0

    Default Re: Probably doing something stupid

    Ah I see and feel stupid now ^^ Many thanks!

Similar Threads

  1. Stupid Array are Stupid.
    By Army in forum New To Java
    Replies: 10
    Last Post: 04-13-2012, 06:51 PM
  2. Stupid mistake, maybe?
    By knox in forum New To Java
    Replies: 1
    Last Post: 11-10-2011, 08:39 AM
  3. Replies: 0
    Last Post: 02-15-2011, 11:16 PM
  4. I need an idea with something really stupid
    By blf_titi in forum Threads and Synchronization
    Replies: 2
    Last Post: 10-27-2010, 03:24 AM
  5. Stupid error
    By dewitrydan in forum Java Applets
    Replies: 3
    Last Post: 08-09-2010, 01:29 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
  •