View Full Version : Clear Button Not working

05-01-2008, 12:13 AM
Im writing a Java program for a hospital billing system. Basically when a user enters information in my text fields I want them to be able to click a clear button and clear all of their entered information. However, my clear button wont work, but it doesn't give me errors when Im writing the code (I use JBuilder to write). When I click the clear button it doesnt clear and gives this:

xception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Primatech$ClearListener.actionPerformed(Primatech.java:231)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1849)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2169)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:420)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:258)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.java:5488)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3126)
at java.awt.Component.processEvent(Component.java:5253)
at java.awt.Container.processEvent(Container.java:1966)
at java.awt.Component.dispatchEventImpl(Component.java:3955)
at java.awt.Container.dispatchEventImpl(Container.java:2024)
at java.awt.Component.dispatchEvent(Component.java:3803)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4212)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3892)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3822)
at java.awt.Container.dispatchEventImpl(Container.java:2010)
at java.awt.Window.dispatchEventImpl(Window.java:1778)
at java.awt.Component.dispatchEvent(Component.java:3803)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:463)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:163)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)

Here's My Code:

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import javax.swing.border.*;

public class Primatech extends JFrame {

JTextField jtxtFirst;
JButton jbtClear;

/** Main method */
public static void main(String[] args) {
Primatech frame = new Primatech();
frame.setTitle("Primatech Health Services");
frame.setSize(600, 420);

public Primatech()

Container c = getContentPane();
// Set GridLayout, 5 rows, 3 columns, and gaps 3 between
c.setLayout(new BorderLayout(1, 1));

//Colors for Program
Color c1 = new Color(000, 000, 000);
Color c2 = new Color(234, 234, 230);
Color c3 = new Color(255, 255, 204);

//Set Fonts for Program
Font serifFont = new Font( "Sans_Serif", Font.BOLD, 12 );
Font titleFont = new Font ( "Serif", Font.BOLD, 26);

//Declared JPanels
JPanel jpLabels = new JPanel();
JPanel jpTextFields = new JPanel();
JPanel jpCB = new JPanel();
JPanel jpInfo = new JPanel();
JPanel jpPay = new JPanel();
JPanel jpButtons = new JPanel();
JPanel jpBillArea = new JPanel();
JPanel jpMenu = new JPanel();

//Declare Borders
Border rBB= new TitledBorder( "Payment Type" );
Border rTF= new TitledBorder ( "Patient Information");
Border rPB= new TitledBorder ( "Patient Bill");

//-----------------MENU BAR----------------------------
JMenuBar newBar = new JMenuBar();

//Adding Help to Menu Bar
JMenu helpMenu = new JMenu("Help");
helpMenu.add(new JMenuItem("1-800-HELPMEE"));
helpMenu.add(new JMenuItem("www.btvs.com/techsupp"));

//Declare Title Primatech Services
JLabel titleLabel = new JLabel("Primatech Services");
titleLabel.setFont( titleFont);

//Create Image Icon
ImageIcon aPlus = new ImageIcon("grdaplus.jpg");
JLabel jlblA = new JLabel(aPlus);

jpMenu.setLayout(new BorderLayout());
jpMenu.setBackground( c3 );
jpMenu.add(jlblA, BorderLayout.EAST);
jpMenu.add(titleLabel, BorderLayout.WEST);
jpMenu.add(newBar, BorderLayout.NORTH);

//-----------------COMBO BOX----------------------------
//Array of States and Doctors for Combo Box
String states[] = { "State:", "GA", "FL", "CO", "NM","OH", "MI", "NY", "PA","MA", "HW" };
String doctor[] = { "Doctor:", "Sylar", "Suresh"};

//Combo Box
JComboBox jcombo1 = new JComboBox(states);
jcombo1.setBackground( c3 );
JComboBox jcombo2 = new JComboBox(doctor);
jcombo2.setBackground( c3 );

//Area to Display Patient Info
jpBillArea.setLayout(new BorderLayout());
JTextArea billArea = new JTextArea(10, 10);
jpBillArea.setBackground( c3 );

//-----------------CHECK BOXES----------------------------
//Check Box
jpCB.setLayout(new BorderLayout());
JCheckBox jcb1 = new JCheckBox("Payment Received?");
jcb1.setBackground( c3 );
JCheckBox jcb2 = new JCheckBox("Insure Claim Filed?");
jcb2.setBackground( c3 );
jpCB.add(jcb1, BorderLayout.EAST);
jpCB.add(jcb2, BorderLayout.WEST);

//-----------------RADIO BUTTONS----------------------------
//Declare Radio Buttons
JRadioButton jrb1 = new JRadioButton("Cash");
JRadioButton jrb2 = new JRadioButton("Check");
JRadioButton jrb3 = new JRadioButton("Credit Card");

//Create JPanel with Radio buttons added
JPanel jpRadioButtons = new JPanel();
jpRadioButtons.setLayout(new BorderLayout(3,1));
jpRadioButtons.setBackground( c3 );
jpRadioButtons.add(jrb1 = new JRadioButton("Cash"), BorderLayout.EAST);
jpRadioButtons.add(jrb2 = new JRadioButton("Check"), BorderLayout.WEST);
jpRadioButtons.add(jrb3 = new JRadioButton("Credit Card"), BorderLayout.CENTER);

//Set BG color for radio Buttons
jrb1.setBackground( c3 );
jrb2.setBackground( c3 );
jrb3.setBackground( c3 );

//Create Group of Radio Buttons
ButtonGroup btg = new ButtonGroup();

JLabel jlblFirst = new JLabel("First Name:");
JLabel jlblLast = new JLabel("Last Name:");
JLabel jlblAddress = new JLabel("Address:");
JLabel jlblCity = new JLabel("City:");
JLabel jlblState = new JLabel("State:");
JLabel jlblZip = new JLabel("Zip Code:");
JLabel jlblSSN = new JLabel("SSN:");
JLabel jlblDoctor = new JLabel("Doctor Name:");
JLabel jlblApp = new JLabel("Appointment Date:");

//Add Jlabels to JLabels Panel
jpLabels.setLayout(new GridLayout(9, 1));
jpLabels.setBackground( c3 );

//-----------------TEXT FIELDS----------------------------
//Declare Text Fields
JTextField jtxtFirst = new JTextField("First Name");
JTextField jtxtLast = new JTextField("", 15);
JTextField jtxtAddress = new JTextField("", 15);
JTextField jtxtCity = new JTextField("", 15);
JTextField jtxtZip = new JTextField("", 15);
JTextField jtxtSSN = new JTextField("", 15);
JTextField jtxtApp = new JTextField("", 15);

jpTextFields.setLayout(new GridLayout(9, 1));
jpTextFields.setBackground( c3 );

jpInfo.setLayout(new BorderLayout());
jpInfo.setBackground( c3 );
jpInfo.add(jpLabels, BorderLayout.WEST);
jpInfo.add(jpTextFields, BorderLayout.EAST);

//Display Bill Button
JButton jbt1 = new JButton("Display Bill");
jbt1.setBackground( c2 );
jbt1.setForeground( c1 );
jbt1.setFont( serifFont );

//Clear Bill Button
JButton jbtClear = new JButton("Clear Bill");
jbtClear.setBackground( c2 );
jbtClear.setForeground( c1 );
jbtClear.setFont( serifFont );

jpButtons.setLayout(new FlowLayout());
jpButtons.setBackground( c3 );
jbtClear.addActionListener( new ClearListener() );

jpPay.setLayout(new BorderLayout());
jpPay.setBackground( c3 );
jpPay.add(jpRadioButtons, BorderLayout.SOUTH);
jpPay.add(jpCB, BorderLayout.CENTER);
jpPay.add(billArea, BorderLayout.NORTH);

// Add labels and text fields to the frame
c.setBackground( c3 );
add (jpMenu, BorderLayout.NORTH);
add (jpInfo, BorderLayout.WEST);
add (jpPay, BorderLayout.EAST);
add (jpButtons, BorderLayout.SOUTH);


private class ClearListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
} // end actionPerformed

}// end ClearListener class


The first name text field should be the only one that works, didnt want to write it out for all of them...

Any suggestions?

05-01-2008, 06:44 PM

So the problem is that in your Primatech constructor at this line:

JTextField jtxtFirst = new JTextField("First Name");

At the beginning of your class you declare a JTextField jtxtFirst as global, yes, but when you go into the constructor you declare this LOCAL variable jtxtFirst and work on it.

So basically, the jtxtFirst that you are working on in the constructor is only used in local scope, but never seen outside of it.

The fix is easy

jtxtFirst = new JTextField("First Name");

05-01-2008, 08:21 PM
Thank you! That worked perfectly!

05-01-2008, 09:25 PM
Ok, one more question, I have correctly been able to clear my two JComboBox as well as my two JCheckBox, but I'm having trouble clearing my radio button group. How would I go about doing this?

I was trying
btg.setSelected( false );

But thats not it and the API isnt helping me out too much...

05-01-2008, 11:22 PM
nevermind, I found it. I had to make an invisible radio button and set that to true when the clear button is pressed!

10-13-2009, 07:49 AM
what should i do if my radio buttons cannot be clear when i click on my clear button?