Results 1 to 4 of 4
  1. #1
    Aiquoc is offline Member
    Join Date
    Mar 2011
    Posts
    7
    Rep Power
    0

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

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    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?

  3. #3
    Aiquoc is offline Member
    Join Date
    Mar 2011
    Posts
    7
    Rep Power
    0

    Default

    the input panel is initialise in a separate class. yeh I think thats the problem.
    Thanks

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    The thing is that input is a variable and it has to be given an initial value, probably in the Crimes constructor.

    Java Code:
    input = new InputPanel();
    The other class defines the input panel constructor but you still have to call it.

Similar Threads

  1. Get value of JComboBox text field
    By nik_meback in forum AWT / Swing
    Replies: 0
    Last Post: 01-07-2011, 03:48 PM
  2. clearing/reseting text fields in GUI
    By reis3k in forum New To Java
    Replies: 1
    Last Post: 12-02-2010, 07:12 PM
  3. Text-field templates.
    By jdipierro in forum New To Java
    Replies: 4
    Last Post: 05-14-2010, 01:48 AM
  4. Refreshing Text Field Value
    By nehakuls in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 11-13-2009, 12:05 PM
  5. Regarding Text Field
    By adeeb in forum AWT / Swing
    Replies: 1
    Last Post: 06-06-2008, 12:01 AM

Posting Permissions

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