Results 1 to 4 of 4

Thread: need help !!!

  1. #1
    Koazimodo_c is offline Member
    Join Date
    Apr 2009
    Posts
    2
    Rep Power
    0

    Default need help !!! JButton, JComboBox, JTextField

    my full code is here when i click on button (B1) i want to display a text on JTextField TF1 but i get an error. what do i need ??
    .
    ..
    ...
    JButton B1 = new JButton("Submit");
    c.fill = GridBagConstraints.NONE;
    B1.setPreferredSize(new Dimension(100,25));
    c.gridx = 0;
    c.gridy = 4;
    pane.add(B1, c);

    JButton B2 = new JButton("Reset");
    c.fill = GridBagConstraints.NONE;
    B2.setPreferredSize(new Dimension(100,25));
    c.gridx = 1;
    c.gridy = 4;
    pane.add(B2, c);

    JTextField TF1=new JTextField("y",50);
    TF1.setEditable(true);
    c.ipady = 100; //make this component tall
    c.weightx = 0.0;
    c.gridwidth = 3;
    c.gridx = 0;
    c.gridy = 5;
    pane.add(TF1, c);

    B1.addActionListener(new listener1());
    B2.addActionListener(new listener2());

    jf.setDefaultCloseOperation(jf.EXIT_ON_CLOSE);
    jf.setVisible(true);

    }

    class listener1 implements ActionListener
    {
    public void actionPerformed(ActionEvent e)
    {
    System.out.println("submit");
    problem==> TF1.setText("submit");
    }
    }
    class listener2 implements ActionListener
    {
    public void actionPerformed(ActionEvent e)
    {
    System.out.println("reset");
    }
    }
    }
    and the error mesage is

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at MyGUI$listener1.actionPerformed(MyGUI.java:162)
    at javax.swing.AbstractButton.fireActionPerformed(Abs tractButton.java:1995)
    at javax.swing.AbstractButton$Handler.actionPerformed (AbstractButton.java:2318)
    at javax.swing.DefaultButtonModel.fireActionPerformed (DefaultButtonModel.java:387)
    at javax.swing.DefaultButtonModel.setPressed(DefaultB uttonModel.java:242)
    at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(BasicButtonListener.java:236)
    at java.awt.Component.processMouseEvent(Component.jav a:6134)
    at javax.swing.JComponent.processMouseEvent(JComponen t.java:3265)
    at java.awt.Component.processEvent(Component.java:589 9)
    at java.awt.Container.processEvent(Container.java:202 3)
    at java.awt.Component.dispatchEventImpl(Component.jav a:4501)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2081)
    at java.awt.Component.dispatchEvent(Component.java:43 31)
    at java.awt.LightweightDispatcher.retargetMouseEvent( Container.java:4301)
    at java.awt.LightweightDispatcher.processMouseEvent(C ontainer.java:3965)
    at java.awt.LightweightDispatcher.dispatchEvent(Conta iner.java:3895)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2067)
    at java.awt.Window.dispatchEventImpl(Window.java:2458 )
    at java.awt.Component.dispatchEvent(Component.java:43 31)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java: 599)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:269)
    at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:184)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:174)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:169)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:161)
    at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:122)
    Last edited by Koazimodo_c; 04-02-2009 at 10:13 PM.

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

    Default

    You likely have two TF1 objects, one that is declared in the class and never initialized and one declared in the method or constructor whose partial code we see above. Please realize that the two objects are completely unrelated, that while you are constructing the one in the constructor (or method) and adding it to your app, that this does nothing to the other one that you likely have in your class.

    what if you change this line:
    Java Code:
    JTextField TF1=new JTextField("y",50);
    to this:
    Java Code:
    TF1=new JTextField("y",50);
    ?

  3. #3
    Koazimodo_c is offline Member
    Join Date
    Apr 2009
    Posts
    2
    Rep Power
    0

    Default thanks alot!

    yea you were right ! i declare the object twice. thanks for your help and post!

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

Posting Permissions

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