Programming buttons

Exception in thread "main" java.lang.NullPointerExceptio

Page 1 of 1

3 Replies - 5419 Views - Last Post: 30 December 2006 - 10:55 AM Rate Topic: -----

#1 buddy04  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 14
  • Joined: 29-November 06

Programming buttons

Posted 30 December 2006 - 06:43 AM

I searched and researched how to program the cancel button. The program compiles ok but when I run it I get an error:

Exception in thread "main" java.lang.NullPointerException
at java.awt.Container.addImpl(Container.java:625)
at java.awt.Container.add(Container.java:307)
at WEEK4Payroll.<init>(WEEK4Payroll.java:225)
at WEEK4Payroll.main(WEEK4Payroll.java:304)

Again, I searched and researched not quite understanding the error. If someone be so kind as to explain the error and point out where I went wrong in the code, it would be greatly appreciated. I have been at this for several days reading and re-reading books, threads ect.

As you can see from the code I tried several ways of programming the action on the cancel button which are commented out.













import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.JComboBox;
import javax.swing.JTextField;
import java.awt.event.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.Date;
import java.text.SimpleDateFormat;


public class WEEK4Payroll extends JFrame {


	//Windows Objects
	private JTextField dateField, ssnField, LnameField, FnameField, TimeInField1, TimeInField2, TimeInField3,
					   TimeInField4, TimeInField5, TimeInField6, TimeOutField1, TimeOutField2,  TimeOutField3,
					   TimeOutField4, TimeOutField5, TimeOutField6, TTimeField1, TTimeField2,   TTimeField3,
					   TTimeField4, TTimeField5, TTimeField6, SickLeaveField, PersonalLeaveField, TDailyHourField,
					   TPayField;



	private JLabel userinfoLabel, dateLabel, datefLabel, ssnLabel, ssnfLabel, LnameLabel, FnameLabel, timesheetLabel, timeinLabel, timeoutLabel,
				   TtimeLabel, sickleaveLabel, personalleaveLabel, TdailyhourLabel, TpayLabel, EmptyLabel2,
				   EmptyLabel3, EmptyLabel4, EmptyLabel5, EmptyLabel6, EmptyLabel7;

	private JPanel UserInfoPanel, ButtonPanel, TimeSheetPanel, EmpInfoPanel;

	private JButton CalcButton, cancelButton;

	private JComboBox ssnComboBox;

	private String ssn [] = { "Select a SSN", "123-45-6789", "234-56-7890", "345-67-8901", "456-78-9012", "567-89-0123"};

	private String currentSSN;

	//private ExitBHandler ebHandler;

	//private CalculateBHandler cbHandler;

	public WEEK4Payroll () {

		//Labels

		dateLabel = new JLabel( "Date  ", SwingConstants.RIGHT );
		datefLabel = new JLabel ( "Enter date as mm-dd-yyyy." );
		ssnLabel = new JLabel( "SSN  ", SwingConstants.RIGHT );
		ssnfLabel = new JLabel ( "" );
		LnameLabel = new JLabel( "Last Name  ", SwingConstants.RIGHT );
		FnameLabel = new JLabel( "First Name  ", SwingConstants.RIGHT );
		timesheetLabel = new JLabel ( "Time Sheet Info" );
		timeinLabel = new JLabel ( "			 Time In" );
		timeoutLabel = new JLabel ( "			 Time Out" );
		TtimeLabel = new JLabel ( "		  Total Time" );
		sickleaveLabel = new JLabel ( "Sick Leave  ", SwingConstants.RIGHT );
		personalleaveLabel = new JLabel ( "Personal Leave  ", SwingConstants.RIGHT );
		TdailyhourLabel = new JLabel ( "Daily Hour Total  ", SwingConstants.RIGHT );
		TpayLabel = new JLabel ( "Total Pay  ", SwingConstants.RIGHT );
		EmptyLabel2 = new JLabel ("");
		EmptyLabel3 = new JLabel ("");
		EmptyLabel4 = new JLabel ("");
		EmptyLabel5 = new JLabel ("");
		EmptyLabel6 = new JLabel ("");
		EmptyLabel7 = new JLabel ("");


		//Text Fields
		//Date
		Date currentDate = new Date();
		SimpleDateFormat today = new SimpleDateFormat("MM-dd-yyyy");	//Prefill current date
		dateField = new JTextField(today.format(currentDate), 10);		//Format date mm/dd/yyyy
		dateField.setEditable( true );

		//ssnField = new JTextField(10);
		ssnComboBox = new JComboBox ( ssn );
		ssnComboBox.setEditable( false );

		LnameField = new JTextField(10);
		LnameField.setEditable( false );

		FnameField = new JTextField(10);
		FnameField.setEditable( false );

		TimeInField1 = new JTextField(10);
		TimeInField2 = new JTextField(10);
		TimeInField3 = new JTextField(10);
		TimeInField4 = new JTextField(10);
		TimeInField5 = new JTextField(10);
		TimeInField6 = new JTextField(10);
		TimeOutField1 = new JTextField(10);
		TimeOutField2 = new JTextField(10);
		TimeOutField3 = new JTextField(10);
		TimeOutField4 = new JTextField(10);
		TimeOutField5 = new JTextField(10);
		TimeOutField6 = new JTextField(10);

		//Total Time Fields
		TTimeField1 = new JTextField(10);
		TTimeField1.setEditable( false );

		TTimeField2 = new JTextField(10);
		TTimeField2.setEditable( false );

		TTimeField3 = new JTextField(10);
		TTimeField3.setEditable( false );

		TTimeField4 = new JTextField(10);
		TTimeField4.setEditable( false );

		TTimeField5 = new JTextField(10);
		TTimeField5.setEditable( false );

		TTimeField6 = new JTextField(10);
		TTimeField6.setEditable( false );

		SickLeaveField = new JTextField(10);
		PersonalLeaveField = new JTextField(10);
		TDailyHourField = new JTextField(10);
		TPayField = new JTextField(10);

		//Buttons OKButton, cancelButton
		//CalcButton = new JButton("Calculate");
		//CalcButton.setActionCommand("GO");

		//cancelButton = new JButton("Cancel");
		//cancelButton.setActionCommand("Cancel");

		/*SPecify handlers for each button and add (register) ActionListeners to each button.
		CalcButton = new JButton("Calculate");
		cbHandler = new CalculateBHandler();
		CalcButton.addActionListener(cbHandler);*/

		cancelButton = new JButton("Cancel");
		//ebHandler = new ExitBHandler();
		//cancelButton.addActionListener(new ExitBHandler()); //setVisible(true);


	//Construct and populate the north panel
	Container mainWindow = getContentPane();
	mainwindow.setLayout( new BorderLayout());




		//Add EmpInfoPanel to Screen
		JPanel UserInfoPanel = new JPanel();
		UserInfoPanel.setBorder(BorderFactory.createCompoundBorder(
		BorderFactory.createTitledBorder("Employee Information"),
		BorderFactory.createEmptyBorder(5,5,30,30)));
		UserInfoPanel.setLayout(new GridLayout(4,2, 5, 1));

		UserInfoPanel.add( dateLabel );		//Date
		UserInfoPanel.add( dateField );
		UserInfoPanel.add( datefLabel );

		UserInfoPanel.add( ssnLabel );		//SSN
		UserInfoPanel.add( ssnComboBox );
		UserInfoPanel.add( ssnfLabel );

		UserInfoPanel.add( LnameLabel );	//Last Name
		UserInfoPanel.add( LnameField );
		UserInfoPanel.add( EmptyLabel2 );

		UserInfoPanel.add( FnameLabel );	//First Name
		UserInfoPanel.add( FnameField );
		UserInfoPanel.add( EmptyLabel3 );

		mainwindow.add (UserInfoPanel, BorderLayout.NORTH);


		//Add TimeSheetPanel to Screen
		JPanel TimeSheetPanel = new JPanel();
		TimeSheetPanel.setBorder(BorderFactory.createCompoundBorder(
		BorderFactory.createTitledBorder("Time Sheet - Enter time in 24-hour format (0815 or 1335)"),
		BorderFactory.createEmptyBorder(5,5,30,30)));
		TimeSheetPanel.setLayout(new GridLayout(11,3, 5, 1));
		TimeSheetPanel.add( timeinLabel );
		TimeSheetPanel.add( timeoutLabel );
		TimeSheetPanel.add( TtimeLabel );
		TimeSheetPanel.add( TimeInField1 );
		TimeSheetPanel.add( TimeOutField1 );
		TimeSheetPanel.add( TTimeField1 );
		TimeSheetPanel.add( TimeInField2 );
		TimeSheetPanel.add( TimeOutField2 );
		TimeSheetPanel.add( TTimeField2 );
		TimeSheetPanel.add( TimeInField3 );
		TimeSheetPanel.add( TimeOutField3 );
		TimeSheetPanel.add( TTimeField3 );
		TimeSheetPanel.add( TimeInField4 );
		TimeSheetPanel.add( TimeOutField4 );
		TimeSheetPanel.add( TTimeField4 );
		TimeSheetPanel.add( TimeInField5 );
		TimeSheetPanel.add( TimeOutField5 );
		TimeSheetPanel.add( TTimeField5 );
		TimeSheetPanel.add( TimeInField6 );
		TimeSheetPanel.add( TimeOutField6 );
		TimeSheetPanel.add( TTimeField6 );
		TimeSheetPanel.add( EmptyLabel4 );
		TimeSheetPanel.add( sickleaveLabel );
		TimeSheetPanel.add( SickLeaveField );
		TimeSheetPanel.add( EmptyLabel5 );
		TimeSheetPanel.add( personalleaveLabel );
		TimeSheetPanel.add( PersonalLeaveField );
		TimeSheetPanel.add( EmptyLabel6 );
		TimeSheetPanel.add( TdailyhourLabel );
		TimeSheetPanel.add( TDailyHourField );
		TimeSheetPanel.add( EmptyLabel7);
		TimeSheetPanel.add( TpayLabel );
		TimeSheetPanel.add( TPayField );
		mainwindow.add (TimeSheetPanel, BorderLayout.CENTER);

		//Add ButtonPanel to Screen
		JPanel ButtonPanel = new JPanel();
		ButtonPanel.add ( CalcButton );
		ButtonPanel.add ( cancelButton );
		mainwindow.add ( ButtonPanel, BorderLayout.SOUTH);

		//ComboBox Action Event
		ssnComboBox.setMaximumRowCount( 6 );	//display 6 rows
		ssnComboBox.addActionListener(
					new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					JComboBox ssncombo = (JComboBox)e.getSource();
					currentSSN = (String)ssncombo.getSelectedItem();

				//IF-Else Statements to auto-fill First Name and Last Name by selecting SSN
				if (currentSSN == ssn[0])
					{LnameField.setText("");
					FnameField.setText("");}
					else
					  if (currentSSN == ssn[1])
						{LnameField.setText("  Jack");
						FnameField.setText("  Jill");}
						else
						  if (currentSSN == ssn[2])
							{LnameField.setText("  Smith");
							FnameField.setText("  Todd");}
							else
							  if (currentSSN == ssn[3])
								{LnameField.setText("  Jones");
								FnameField.setText("  Bob");}
								else
								  if (currentSSN == ssn[4])
									{LnameField.setText("  James");
									FnameField.setText("  Dennis");}
									else
									  if (currentSSN == ssn[5])
										{LnameField.setText("  Klumm");
										FnameField.setText("  Harry");}

						}
					}
		);

			cancelButton.addActionListener(new ActionListener()
			  {
   				 public void actionPerformed(ActionEvent e)
	 			{
					System.exit(0);
				  }
			  });

		/*TimeInField1.addActionListener(
					 new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					JTextField TimeInField1 = (JTextField)e.getSource();
					TimeIn1 = (double)TimeInField1.getSelectedItem();

					TimeInField2.setText(TimeIn1);

					}
				}
		); */





		}

		/*class ExitBHandler implements ActionListener
			{
				public void actionPerformed(ActionEvent e)
				{
					System.exit(0);
				}
   			 }*/

	public static void main(String args[])
		{

		//Create the frame
		JFrame frame = new WEEK4Payroll();
		frame.setTitle("Week 4 Payroll Program");
		frame.setSize(600, 500);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setLocation(200, 100);
		frame.setVisible(true);
		}





}




Is This A Good Question/Topic? 0
  • +

Replies To: Programming buttons

#2 horace  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 291
  • View blog
  • Posts: 1,900
  • Joined: 25-October 06

Re: Programming buttons

Posted 30 December 2006 - 07:55 AM

if I try to run your program it gives nullPointerException on line
		ButtonPanel.add ( CalcButton );


because all the places where you
CalcButton = new JButton("Calculate");

are commented out?

If I uncomment the line the program then shows the GUI
Was This Post Helpful? 0
  • +
  • -

#3 buddy04  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 14
  • Joined: 29-November 06

Re: Programming buttons

Posted 30 December 2006 - 10:29 AM

Thanks Horace. The two lines you identified as being the problem did you use a tool that point out bad code? If so, what tool do you use and how do I get it? Is it a debugging tool?
Was This Post Helpful? 0
  • +
  • -

#4 horace  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 291
  • View blog
  • Posts: 1,900
  • Joined: 25-October 06

Re: Programming buttons

Posted 30 December 2006 - 10:55 AM

View Postbuddy04, on 30 Dec, 2006 - 05:29 PM, said:

Thanks Horace. The two lines you identified as being the problem did you use a tool that point out bad code? If so, what tool do you use and how do I get it? Is it a debugging tool?

the message indicates line in your code were the error occured, e.g.
Exception in thread "main" java.lang.NullPointerException
at java.awt.Container.addImpl(Container.java:625)
at java.awt.Container.add(Container.java:307)
at WEEK4Payroll.<init>(WEEK4Payroll.java:225)
at WEEK4Payroll.main(WEEK4Payroll.java:304)

in this case you look at line 225 in file WEEK4Payroll.java and the error being NullPointerException means some object on that line has not been created with a new. You then work back from that. In this case it was fairly clear where the problem was but if the line contains many references to objects you can always put a System.out.println() before the line to print out the values of the object to narrow it down further.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1