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)

did you enter a value in all the input fields you are trying to get a value from?

Yes, in the cal and fat text fields, and the output is suppose to display in the per text field after you use the Calculate button.

Edited 4 Years Ago by makehaste: n/a

java.lang.NullPointerException
at FatGramsPanel$FatListener.actionPerformed(FatGramsPanel.java:54)

What variable has a null value on line 54?
Then why is its value null?

Do you know the difference between local and class variables and that local variables will shadow the class variables?

Check that you have not defined any variables in two places: class and local.

Edited 4 Years Ago by NormR1: n/a

think I know what your problem is: you're re-declaring your instance variable in the scope of your constructor, so that's the only place where it can be read.

think I know what your problem is: you're re-declaring your instance variable in the scope of your constructor, so that's the only place where it can be read.

Yep, that was the problem. Thank you so much.

This question has already been answered. Start a new discussion instead.