0

what's wrong with my java code?

NullPointerException was found by my compiler....

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

class WheelCalc extends JFrame implements ActionListener
{
	
	public static final int WIDTH = 400;
	public static final int HEIGHT = 400;
		public JLabel NewTire;
		public JLabel OldTire;
		public JTextField txtNew;
		public JTextField txtOld;
		public JLabel lblActualSpeed;
		public JTextField txtspeed;
		public JButton cmdEnter;

	WheelCalc()
	{
		super("Tire TO speed calculation v0.1");

	
		Container c = getContentPane();
		
		c.setLayout( new GridLayout(4, 3));
		
		NewTire = new JLabel("New Tire Diameter: ");
		txtNew = new JTextField(8);
		OldTire = new JLabel("Old Tire Diameter: ");
		txtOld = new JTextField(8);

		lblActualSpeed = new JLabel(" . ");
		cmdEnter =  new JButton("Enter");
		cmdEnter.addActionListener(this);
		cmdEnter.setActionCommand("calculate");
			
		c.add(NewTire);
		c.add(txtNew);
		c.add(txtspeed);
		c.add(lblActualSpeed);
		c.add(cmdEnter);
				
		setVisible(true);
		setSize(WIDTH, HEIGHT);
		setDefaultCloseOperation(EXIT_ON_CLOSE);

	}
		public void actionPerformed(ActionEvent e)
		{
		  double tire_diameter, new_tire, speed, actualSpeed;
		  
			if ("calculate".equals(e.getActionCommand()))
			{
			   tire_diameter = Double.parseDouble(txtOld.getText());
				
				new_tire = Double.parseDouble(txtNew.getText());
				
			 	speed =  Double.parseDouble(txtspeed.getText());
				
				actualSpeed = (new_tire / tire_diameter) * speed;
				
				lblActualSpeed.setText("Actual Speed: " + actualSpeed + "MPH");
			}
		}
	public static void main(String args[])
	{
	 WheelCalc WheelspeedCalc = new WheelCalc();
	}
}

Exception in thread "main" java.lang.NullPointerException
at java.awt.Container.addImpl(Container.java:1041)
at java.awt.Container.add(Container.java:365)
at WheelCalc.<init>(WheelCalc.java:49)
at WheelCalc.main(WheelCalc.java:77)

Edited by SCoder1: forgot error messages

2
Contributors
1
Reply
2
Views
6 Years
Discussion Span
Last Post by stultuske
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.