2 Replies - 213 Views - Last Post: 03 April 2013 - 04:25 AM Rate Topic: -----

#1 zorak  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 8
  • Joined: 05-February 12

Exception in thread "main" error

Posted 03 April 2013 - 04:09 AM

Hey guys, I am getting the error below and am stuck trying to figure it out. I've looked at line 45 but don't see what's wrong. Can anyone point me to what I need to fix?

Exception in thread "main" java.lang.NullPointerException
at OfficeAreaCalculator.<init>(OfficeAreaCalculator.java:45)
at testOfficeAreaCalculator.main(testOfficeAreaCalculator.java:8)

import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.text.DecimalFormat;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class OfficeAreaCalculator extends JFrame {

	private static final long serialVersionUID = 123456;
	private JFrame mainFrame;
	private JButton calculateButton;
	private JButton exitButton;
	private JTextField lengthField;
	private JTextField widthField;
	private JTextField areaField;
	private JLabel lengthLabel;
	private JLabel widthLabel;
	private JLabel areaLabel;

	// class constructor method
	public OfficeAreaCalculator() {

		exitButton = new JButton("Exit");
		lengthLabel = new JLabel("Enter length of the office:");
		widthLabel = new JLabel("Enter width of the office:");
		areaLabel = new JLabel("Office area:");
		lengthField = new JTextField(5);// 5 is the size of the text field
		widthField = new JTextField(5);
		areaField = new JTextField(5);
		calculateButton = new JButton("Calculate");

		// get a container for the frame
		Container c = mainFrame.getContentPane();

		// set the layout for the container
		c.setLayout(new FlowLayout());

		// add components to the container

		// set the mnemonic for each button

		// set the size of the GUI frame
		mainFrame.setSize(260, 150);

		// makes sure the program terminates when the application is closed
		mainFrame.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {

		// add action listener to each button
		// calculateButtonHandler is a class that is defined below
		CalculateButtonHandler chandler = new CalculateButtonHandler();

		// ExitButtonHandler is a class that is defined below
		ExitButtonHandler ehandler = new ExitButtonHandler();

		// FocusHandler is a class that is defined below
		FocusHandler fhandler = new FocusHandler();

		// set the GUI frame visible
	}// end constructor method OfficeAreaCalculator
	 *This class implements the interface ActionListener
	 *It is a class that will handle the action on the 
	 *Calculate button. The required method ActionPerformed
	 *is created to read the input values of the input fields
	 *and calculate the area of an office given the dimensions 
	class CalculateButtonHandler implements ActionListener{
		public void actionPerformed(ActionEvent e){
			DecimalFormat num = new DecimalFormat(",###.##");
			double width,length, area;
			String instring;
			instring = lengthField.getText();
			if (instring.equals(""))
				instring = ("0");
			instring = widthField.getText();
			if (instring.equals(""))
				instring = "0";
			width = Double.parseDouble(instring);
			area = length*width;

		}//end ActionPerformed
	}//end class Calculate ButtonHandler
	 * This class implements the ActionListener interface
	 * and handles the action on the Exit Button
	class ExitButtonHandler implements ActionListener{
		public void actionPerformed(ActionEvent e){
	}//end class ExitButtonHandler
	 * This class implements the FocusListener interface
	 * and handles the focus of the components.
	class FocusHandler implements FocusListener{
		public void focusGained(FocusEvent e){
			if (e.getSource()==lengthField || e.getSource()== widthField)
			else if (e.getSource()==areaField)
		}//end focusGained
		public void focusLost(FocusEvent e){
			if (e.getSource() == widthField)
		}//end focusLost
	}//end class FocusHandler

}// end class OfficeAreaCalculator

Is This A Good Question/Topic? 0
  • +

Replies To: Exception in thread "main" error

#2 zorak  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 8
  • Joined: 05-February 12

Re: Exception in thread "main" error

Posted 03 April 2013 - 04:23 AM

Figured it out! Forgot to create a new JFrame.

mainFrame = new JFrame("Messages");

Problem solved.
Was This Post Helpful? 1
  • +
  • -

#3 Flukeshot  Icon User is offline

  • A little too OCD
  • member icon

Reputation: 401
  • View blog
  • Posts: 999
  • Joined: 14-November 12

Re: Exception in thread "main" error

Posted 03 April 2013 - 04:25 AM

You've forgotten to initialise mainFrame as a JFrame.

mainFrame = new JFrame();

Put this with the rest of the components and it should work.

Edit: I see you found it out yourself, well done :)

This post has been edited by Flukeshot: 03 April 2013 - 04:26 AM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1