home hardware prices news articles forums photos user reviews
Go Back   Tech Support Forums - TechIMO.com > PC Hardware and Tech > Webmastering and Programming
Ask a Tech Support Question (free)!

Clear Button Java Wont Work

Reply
Get bargains at  »  Dealighted.com
 
Thread Tools Search this Thread
Currently Active Users: 1987
Discussions: 200,936, Posts: 2,379,228, Members: 246,301
Old April 30th, 2008, 04:00 PM   Digg it!   #1 (permalink)
Member
 
Ty44ler's Avatar
 
Join Date: Mar 2005
Location: Atlanta, Georgia
Posts: 407
Send a message via AIM to Ty44ler
Clear Button Java Wont Work

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:

Quote:
xception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Primatech$ClearListener.actionPerformed(Primatech. java:231)
at javax.swing.AbstractButton.fireActionPerformed(Abs tractButton.java:1849)
at javax.swing.AbstractButton$Handler.actionPerformed (AbstractButton.java:2169)
at javax.swing.DefaultButtonModel.fireActionPerformed (DefaultButtonModel.java:420)
at javax.swing.DefaultButtonModel.setPressed(DefaultB uttonModel.java:258)
at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.jav a:5488)
at javax.swing.JComponent.processMouseEvent(JComponen t.java:3126)
at java.awt.Component.processEvent(Component.java:525 3)
at java.awt.Container.processEvent(Container.java:196 6)
at java.awt.Component.dispatchEventImpl(Component.jav a:3955)
at java.awt.Container.dispatchEventImpl(Container.jav a:2024)
at java.awt.Component.dispatchEvent(Component.java:38 03)
at java.awt.LightweightDispatcher.retargetMouseEvent( Container.java:4212)
at java.awt.LightweightDispatcher.processMouseEvent(C ontainer.java:3892)
at java.awt.LightweightDispatcher.dispatchEvent(Conta iner.java:3822)
at java.awt.Container.dispatchEventImpl(Container.jav a:2010)
at java.awt.Window.dispatchEventImpl(Window.java:1778 )
at java.awt.Component.dispatchEvent(Component.java:38 03)
at java.awt.EventQueue.dispatchEvent(EventQueue.java: 463)
at java.awt.EventDispatchThread.pumpOneEventForHierar chy(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:163)
at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:157)
at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:149)
at java.awt.EventDispatchThread.run(EventDispatchThre ad.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?
__________________
Frank: Blue do you trust that I do not want to see you die here tonight?
Blue: Yes sir.
Frank: Blue you're my boy!
Ty44ler is offline   Reply With Quote
Old April 30th, 2008, 09:38 PM     #2 (permalink)
Caveat Emptor
 
Rootstonian's Avatar
 
Join Date: Mar 2005
Location: Out of my mind
Posts: 3,241
Send a message via AIM to Rootstonian
Not that familiar with JAVA, but this line may be your problem...(just a WAG "wild a$$ guess)

jbtClear.addActionListener( new ClearListener() );

I say that due to the first line of your error message "....Null pointer exception..."

Can you set a "debug" mode and step through the program?

Or maybe ClearListener needs to return a value???
Rootstonian is offline   Reply With Quote
Old April 30th, 2008, 10:01 PM     #3 (permalink)
Member
 
Ty44ler's Avatar
 
Join Date: Mar 2005
Location: Atlanta, Georgia
Posts: 407
Send a message via AIM to Ty44ler
Hmmm...

Well as a WAT (wild a$$ try)

I gave the ClearListener a value of: jtxtFirst.setText("")

however that didnt work and I get the same thing...
Ty44ler is offline   Reply With Quote
Old May 1st, 2008, 04:08 AM     #4 (permalink)
Super F@D Folder
 
Join Date: Jun 2004
Posts: 5,083
Send a message via AIM to sr71000
try commenting out this line

jtxtFirst.setText("");

replace it with a print out to the console. see if jtxtFirst is null.
sr71000 is offline   Reply With Quote
Old May 1st, 2008, 03:22 PM     #5 (permalink)
Member
 
Ty44ler's Avatar
 
Join Date: Mar 2005
Location: Atlanta, Georgia
Posts: 407
Send a message via AIM to Ty44ler
I finally got it to work.

As what was stated before I didnt need to declare it a second time. Thank you for your help!
Ty44ler is offline   Reply With Quote
Old January 14th, 2009, 06:57 AM     #6 (permalink)
Junior Member
 
Join Date: Jan 2009
Posts: 2
Smile
sandesh nawadkar:-

sandesh nawadkar:- I sujjest you that you have to use getContentPane().add() method
sandesh007 is offline   Reply With Quote
Old March 11th, 2009, 04:05 PM     #7 (permalink)
Junior Member
 
Join Date: Jan 2009
Posts: 2
Smile
sandesh nawadkar answer:-

use jCreator or kava pro that will help u to remove errors.
sandesh007 is offline   Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search

Similar Threads
Thread Thread Starter Forum Replies Last Post
XP wont activate with a genuine key!! Reinstalled windows and now wont work? Help me a1whitey Security and Privacy Issues 9 December 7th, 2006 07:52 AM
my add printer button doesnt work xelnanga General Tech Discussion 2 January 12th, 2006 01:10 AM
java applet doesn't work missymystique General Tech Discussion 2 August 21st, 2005 04:24 AM
pc wont boot unless i clear cmos every time hada Processors, Memory, and Overclocking 3 May 18th, 2004 08:32 AM
java mysteriously gone and wont come back watchtower7 Technical Support 3 January 30th, 2004 03:59 PM


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Most Active Discussions
windows 7 problem (6)
Is It Just Me? (2897)
Wireless Televisions. (8)
California Passes Anti-Flat-HDTV Le.. (41)
Obama the Muslim (14)
Is the PSU I received dead? (10)
windows vista security holes (9)
HIS HD5770 graphic card question (15)
Install XP pro and a Vista laptop ?.. (11)
Print spooler problem (13)
Foreign voltage (10)
Dept. of HS: NSA 'Helped' Develop V.. (15)
A good PSU? (10)
EVGA 9800 gtx help with finding a g.. (8)
Recent Discussions
windows 7 problem (6)
Point and Shoot Camera Suggestions. (2)
Modern Warfare 2 freeze (13)
Wireless Televisions. (8)
CPU fan stops spinning randomly (3)
wireless user (1)
World's largest Monopoly Game using G.. (332)
Ideal cheap graph card for PC-Gaming? (17)
BIOS won't read disk when I try to fl.. (0)
Install XP pro and a Vista laptop ?? (11)
Partition Magic caused HDD problem (2)
Graphics Card Upgrade Question (1)
favorit (1)
solutions for virtical white lines on.. (1)
Regular Build (3)
Fire in DVD (2)
Modern Warfare For the PC (33)
radeon x850xt platinum & shader 3 (3)
Have you switched yet? (84)
Wireless Router+Cable Modems and Much.. (0)
Optical Audio A-B Switch (1)
windows vista security holes (9)
The NTDVM CPU has encountered an ille.. (24)
[F@H SPAM 11/16/09] ! 1/2 months to r.. (34)
Wireless speakers for PC? (11)


All times are GMT -4. The time now is 11:25 AM.
TechIMO Copyright 2009 All Enthusiast, Inc.



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28