Greetings Everyone. I'm working on a program that will take the number of calories and grams of fat, and give the user the percent of calories from fat. The problem arises when I use the Calculate button.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class FatGramsPanel extends JPanel
{
JLabel calorieLabel, fatLabel, percentLabel;
JTextField cal, fat, per;
public FatGramsPanel()
{
calorieLabel = new JLabel ("Enter number of calories:");
fatLabel = new JLabel ("Enter grams of fat");
percentLabel = new JLabel ("% of calories from fat: ");
JTextField cal = new JTextField (7);
JTextField fat = new JTextField (7);
JTextField per = new JTextField (7);
JButton calculate = new JButton("Calculate");
calculate.addActionListener(new FatListener());
JButton exit = new JButton("Exit");
exit.addActionListener(new ExitButton());
add (calorieLabel);
add (cal);
add (fatLabel);
add (fat);
add (percentLabel);
add (per);
add (calculate);
add (exit);
setPreferredSize (new Dimension(250, 110));
}
public class ExitButton implements ActionListener
{
public void actionPerformed (ActionEvent event)
{
System.exit(0);
}
}
public class FatListener implements ActionListener
{
public void actionPerformed (ActionEvent event)
{
double fatcalc, calcalc, percentcalc;
fatcalc = Double.parseDouble(fat.getText());
calcalc = Double.parseDouble(cal.getText());
percentcalc = ((fatcalc * 9.0) / calcalc) * 100.0;
per.setText(String.valueOf(percentcalc));
}
}
}
The error I get is:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at FatGramsPanel$FatListener.actionPerformed(FatGramsPanel.java:54)
at javax.swing.AbstractButton.fireActionPerformed(Unknown 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.mouseReleased(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(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown 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.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)