Results 1 to 4 of 4
- 05-31-2011, 06:52 AM #1
Member
- Join Date
- Mar 2011
- Posts
- 7
- Rep Power
- 0
GUI Clearing Text Field not working
Hi, just trying to clear a text field when I click a button but I have the error
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Crimes$ButtonListener.actionPerformed(Crimes.java: 52)
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:6267)
at javax.swing.JComponent.processMouseEvent(JComponen t.java:3267)
at java.awt.Component.processEvent(Component.java:603 2)
at java.awt.Container.processEvent(Container.java:204 1)
at java.awt.Component.dispatchEventImpl(Component.jav a:4630)
at java.awt.Container.dispatchEventImpl(Container.jav a:2099)
at java.awt.Component.dispatchEvent(Component.java:44 60)
at java.awt.LightweightDispatcher.retargetMouseEvent( Container.java:4577)
at java.awt.LightweightDispatcher.processMouseEvent(C ontainer.java:4238)
at java.awt.LightweightDispatcher.dispatchEvent(Conta iner.java:4168)
at java.awt.Container.dispatchEventImpl(Container.jav a:2085)
at java.awt.Window.dispatchEventImpl(Window.java:2478 )
at java.awt.Component.dispatchEvent(Component.java:44 60)
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)
I really dont know what is wrong, pls help :)
The code for the action listener is:
(This is located in one class). I try to acces the clear method in InputPanel, which clears the text fields.
public class Crimes extends JPanel {
private Prison prison;
private InputPanel input;
public Crimes(Prison prison) {
//this.prison = prison;
setup();
build();
}
private void setup() {
list = new CrimeList();
}
private void build(Prison prison) {
setLayout(new DisplayLayout());
add(list);
add(button("Allocate Cell"));
}
private JButton button(String label){
JButton button = new JButton(label);
button.addActionListener(new ButtonListener());
return button;
}
private class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
input.clear();
}
}
And the code for creating and clearing the Text field is:
(This is in another class)
public class InputPanel extends JPanel {
private JTextField name = nameField();
private JTextField day = fields();
private JTextField month = fields();
private JTextField year = fields();
public InputPanel() {
build();
}
private void build() {
add(new JLabel("Name"));
add(name);
add(day);
add(label("days"));
add(month);
add(label("months "));
add(year);
add(label("years"));
}
public JLabel label(String type) {
JLabel label = new JLabel(type);
label.setPreferredSize(new Dimension(60, 20));
return label;
}
private JTextField nameField() {
JTextField name = new JTextField();
name.setPreferredSize(new Dimension(90, 20));
return name;
}
private JTextField fields() {
JTextField period = new JTextField();
period.setPreferredSize(new Dimension(30, 20));
return period;
}
public void clear() {
name.setText("");
day.setText("");
month.setText("");
year.setText("");
}
}
- 05-31-2011, 07:10 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
You are getting the NPE because input is null when the button gets clicked. So, where is the input panel supposed to be initialised - ie where is input given a nonnull value?
- 05-31-2011, 08:18 AM #3
Member
- Join Date
- Mar 2011
- Posts
- 7
- Rep Power
- 0
the input panel is initialise in a separate class. yeh I think thats the problem.
Thanks
- 05-31-2011, 08:53 AM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
Similar Threads
-
Get value of JComboBox text field
By nik_meback in forum AWT / SwingReplies: 0Last Post: 01-07-2011, 02:48 PM -
clearing/reseting text fields in GUI
By reis3k in forum New To JavaReplies: 1Last Post: 12-02-2010, 06:12 PM -
Text-field templates.
By jdipierro in forum New To JavaReplies: 4Last Post: 05-14-2010, 12:48 AM -
Refreshing Text Field Value
By nehakuls in forum JavaServer Pages (JSP) and JSTLReplies: 2Last Post: 11-13-2009, 11:05 AM -
Regarding Text Field
By adeeb in forum AWT / SwingReplies: 1Last Post: 06-05-2008, 11:01 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks