Volume Calculator Problem
Hello all! I am having a problem with a calculator that calculates the volume of a pool based on user input. Our program runs (its a group program) but our calculate button always gives us this error:Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at FinalProject$calcBtnHandler.actionPerformed(FinalP roject.java:136)
at javax.swing.AbstractButton.fireActionPerformed(Unk nown Source)
at javax.swing.AbstractButton$Handler.actionPerformed (Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed (Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent( Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(U nknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unkno wn Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilter s(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(U nknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarch y(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
this is our program so far (We are going to make another tab with a different calculator but we want to figure the problem with this one first.)
Code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.text.*;
public class FinalProject extends JFrame
{
private JTabbedPane tab;
private JPanel p1;
private JButton calcBtn;
private JButton extBtn;
private JTextField t1;
private JTextField t2;
private JTextField t3;
private JTextField sumField;
private JLabel l1;
private JLabel l2;
private JLabel l3;
private JLabel sumLabel;
public FinalProject()
{
super("Final Project"); //call super JFrame constructor
JTabbedPane tab = new JTabbedPane();
//define components for tab 1
JLabel l1 = new JLabel("Enter the length of the hot tub:");
JLabel l2 = new JLabel("Enter the height of the hot tub:");
JLabel l3 = new JLabel("Enter the depth of the hot tub:");
JTextField t1 = new JTextField(5);
JTextField t2 = new JTextField(5);
JTextField t3 = new JTextField(5);
JButton calcBtn = new JButton("Calculate Volume");
JLabel sumLabel = new JLabel("The hot tub volume is:");
JTextField sumField = new JTextField(13);
JButton extBtn = new JButton("Exit");
JPanel p1 = new JPanel();
// add components to the tab
p1.setBackground(Color.WHITE);
p1.setLayout(new FlowLayout());
p1.add(l1);
p1.add(t1);
p1.add(l2);
p1.add(t2);
p1.add(l3);
p1.add(t3);
p1.add(calcBtn);
p1.add(sumLabel);
p1.add(sumField);
p1.add(extBtn);
tab.addTab("Hot tubs",null, p1, "Panel #1");
getContentPane().add(tab);
setSize(200,350);
setVisible(true);
/*define and register window event handler
p1.addWindowListener(new Window Adapter()
{
public void windowClosing(Window Event e){System.exit(0);}
});
*/
//register listeners
calcBtnHandler chandler = new calcBtnHandler();
calcBtn.addActionListener(chandler);
extBtnHandler ehandler = new extBtnHandler();
extBtn.addActionListener(ehandler);
//create and register the new focus handler
FocusHandler fhandler = new FocusHandler();
t1.addFocusListener(fhandler);
t2.addFocusListener(fhandler);
t3.addFocusListener(fhandler);
sumField.addFocusListener(fhandler);
tab.show();
}
class FocusHandler implements FocusListener
{
public void focusGained(FocusEvent e)
{
if(e.getSource()== t1)
{
t1.setText(""); //blank input textfield
sumField.setText("");//blank text field
}
else if(e.getSource() == t2)
{
t2.setText(""); //blank input textfield
sumField.setText("");//blank textfield
}
else if(e.getSource() == t3)
{
t3.setText("");//blank input textfield
sumField.setText("");//blank field
}
else if(e.getSource() == sumField)
{
sumField.setNextFocusableComponent(t1);
t1.grabFocus();
}
}
public void focusLost(FocusEvent e)
{
if(e.getSource()== t1)
{
t1.setNextFocusableComponent(t2);
}
if(e.getSource()==t2)
{
t2.setNextFocusableComponent(t3);
}
if(e.getSource()==t3)
{
t3.setNextFocusableComponent(calcBtn);
}
}
}
class calcBtnHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
DecimalFormat num =new DecimalFormat(",###.##");
double invalue1, invalue2, invalue3 = 0, outvalue;
String instring;
//sumField.setEnabled(true); //enable area text field
instring =t1.getText(); //read input value
//prevent an entry from an empty string
if(instring.equals(""))
{
instring = "0";
t1.setText("0");
}
invalue1 = Double.parseDouble(instring); //convert to double
instring =t2.getText(); //read second input
//prevent an entry from an empty string
if(instring.equals(""))
{
instring = "0";
t2.setText("0");
}
invalue2 = Double.parseDouble(instring); //convert to double
instring =t3.getText(); //read third input
//prevent an entry from an empty string
if(instring.equals(""))
{
instring = "0";
t3.setText("0");
}
if(e.getSource() == calcBtn)
invalue3 = Double.parseDouble(instring); //convert to double
outvalue = invalue1 * invalue2 * invalue3;
sumField.setText(num.format(outvalue));
}
}
class extBtnHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
}
public static void main(String args[])
{
new FinalProject(); //instanciate a GUI object
}
}
*Also while it says final project it is not. It is purely extra credit and for practice.