Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    Feb 2008
    Posts
    57
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Question Clear Button Not working

    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:
    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.setLocationRelativeTo(null);
    	    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	    frame.setSize(600, 420);
    	    frame.setVisible(true);
    	    }		
    
      public Primatech()
      {
    
        Container c = getContentPane();
        //-----------------ATTRIBUTES----------------------------
        // 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");
        newBar.add(helpMenu);
        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 );
        jpBillArea.add(billArea);
        
        //-----------------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.setBorder(rBB);
        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();
        btg.add(jrb1);
        btg.add(jrb2);
        btg.add(jrb3);
        
        //-----------------LABELS----------------------------
        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 );
        jpLabels.add(jlblFirst);
        jpLabels.add(jlblLast);
        jpLabels.add(jlblAddress);
        jpLabels.add(jlblCity);
        jpLabels.add(jlblState);
        jpLabels.add(jlblZip);
        jpLabels.add(jlblSSN);
        jpLabels.add(jlblDoctor);
        jpLabels.add(jlblApp);
        
        //-----------------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 );
        jpTextFields.add(jtxtFirst);
        jpTextFields.add(jtxtLast);
        jpTextFields.add(jtxtAddress);
        jpTextFields.add(jtxtCity);
        jpTextFields.add(jcombo1);
        jpTextFields.add(jtxtZip);
        jpTextFields.add(jtxtSSN);
        jpTextFields.add(jcombo2);
        jpTextFields.add(jtxtApp);
    
    
        jpInfo.setLayout(new BorderLayout());
        jpInfo.setBorder(rTF);
        jpInfo.setBackground( c3 );
        jpInfo.add(jpLabels, BorderLayout.WEST);
        jpInfo.add(jpTextFields, BorderLayout.EAST);
        
        //----------------BUTTONS----------------------------
        //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 );
        jpButtons.add(jbt1);
        jpButtons.add(jbtClear);
        jbtClear.addActionListener( new ClearListener() );
        
        jpPay.setLayout(new BorderLayout());
        jpPay.setBackground( c3 );
        jpPay.setBorder(rPB);
        jpPay.add(jpRadioButtons, BorderLayout.SOUTH);
        jpPay.add(jpCB, BorderLayout.CENTER);
        jpPay.add(billArea, BorderLayout.NORTH);
        
        //-----------------FRAME----------------------------
        // 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) {
    	      jtxtFirst.setText("");
    	    } // 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?

  • #2
    Regular Coder Aradon's Avatar
    Join Date
    Jun 2005
    Location
    USA
    Posts
    734
    Thanks
    0
    Thanked 20 Times in 19 Posts
    Hey,

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

    Code:
    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

    Code:
    jtxtFirst = new JTextField("First Name");
    "To iterate is human, to recurse divine." -L. Peter Deutsch

  • Users who have thanked Aradon for this post:

    Ty44ler (05-01-2008)

  • #3
    New Coder
    Join Date
    Feb 2008
    Posts
    57
    Thanks
    8
    Thanked 0 Times in 0 Posts
    Thank you! That worked perfectly!

  • #4
    New Coder
    Join Date
    Feb 2008
    Posts
    57
    Thanks
    8
    Thanked 0 Times in 0 Posts
    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
    Code:
    btg.setSelected( false );
    But thats not it and the API isnt helping me out too much...

  • #5
    New Coder
    Join Date
    Feb 2008
    Posts
    57
    Thanks
    8
    Thanked 0 Times in 0 Posts
    nevermind, I found it. I had to make an invisible radio button and set that to true when the clear button is pressed!

  • #6
    wsc
    wsc is offline
    New to the CF scene
    Join Date
    Oct 2009
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    what should i do if my radio buttons cannot be clear when i click on my clear button?

    what should i do if my radio buttons cannot be clear when i click on my clear button?


  •  

    Posting Permissions

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