Results 1 to 4 of 4
Thread: need help !!!
- 04-02-2009, 08:57 PM #1
Member
- Join Date
- Apr 2009
- Posts
- 2
- Rep Power
- 0
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 09:13 PM.
-
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:
to this:Java Code:JTextField TF1=new JTextField("y",50);
?Java Code:TF1=new JTextField("y",50);
- 04-03-2009, 03:48 AM #3
Member
- Join Date
- Apr 2009
- Posts
- 2
- Rep Power
- 0
thanks alot!
yea you were right ! i declare the object twice. thanks for your help and post!
-


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks