Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Java

Zach Sienkiewicz
Zach Sienkiewicz
13,225 Points

Java GUI

I am getting an error that I do not understand: package iLab2;

import java.awt.BorderLayout; import iLab1.HealthProfile; import java.awt.EventQueue;

import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.border.EmptyBorder; import javax.swing.JLabel; import javax.swing.JTextField; import javax.swing.JButton; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent;

public class HealthProfileGUI extends JFrame {

private JPanel contentPane;
private JTextField textFieldName;
private JTextField textFieldAge;
private JTextField textFieldWeight;
private JTextField textFieldHeightFt;
private JTextField textFieldHeightIn;
private JTextField lblMsgBMI;
private JTextField lblMsgCat;
private JTextField lblMsgHr;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                HealthProfileGUI frame = new HealthProfileGUI();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the frame.
 */
public HealthProfileGUI() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 658, 478);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    JLabel lblName = new JLabel("Name:");
    lblName.setBounds(15, 36, 69, 20);
    contentPane.add(lblName);

    JLabel lblAge = new JLabel("Age:");
    lblAge.setBounds(15, 72, 69, 20);
    contentPane.add(lblAge);

    JLabel lblWeight = new JLabel("Weight:");
    lblWeight.setBounds(15, 108, 69, 20);
    contentPane.add(lblWeight);

    JLabel lblHeightInFeet = new JLabel("Height in Feet:");
    lblHeightInFeet.setBounds(15, 144, 106, 20);
    contentPane.add(lblHeightInFeet);

    JLabel lblHeightInInches = new JLabel("Height in Inches:");
    lblHeightInInches.setBounds(15, 180, 121, 20);
    contentPane.add(lblHeightInInches);

    textFieldName = new JTextField();
    textFieldName.setBounds(167, 33, 274, 26);
    contentPane.add(textFieldName);
    textFieldName.setColumns(10);

    textFieldAge = new JTextField();
    textFieldAge.setBounds(167, 69, 146, 26);
    contentPane.add(textFieldAge);
    textFieldAge.setColumns(10);

    textFieldWeight = new JTextField();
    textFieldWeight.setBounds(167, 105, 146, 26);
    contentPane.add(textFieldWeight);
    textFieldWeight.setColumns(10);

    textFieldHeightFt = new JTextField();
    textFieldHeightFt.setBounds(167, 141, 146, 26);
    contentPane.add(textFieldHeightFt);
    textFieldHeightFt.setColumns(10);

    textFieldHeightIn = new JTextField();
    textFieldHeightIn.setBounds(167, 177, 146, 26);
    contentPane.add(textFieldHeightIn);
    textFieldHeightIn.setColumns(10);

    JButton btnCalculate = new JButton("Calculate");
    btnCalculate.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent arg0) {
            calcFitness();
        }
    });
    btnCalculate.setBounds(21, 241, 115, 29);
    contentPane.add(btnCalculate);

    JButton btnClear = new JButton("Clear");
    btnClear.setBounds(372, 241, 115, 29);
    contentPane.add(btnClear);

    JLabel lblBmi = new JLabel("BMI:");
    lblBmi.setBounds(15, 301, 69, 20);
    contentPane.add(lblBmi);

    JLabel lblCategory = new JLabel("Category:");
    lblCategory.setBounds(15, 326, 69, 20);
    contentPane.add(lblCategory);

    JLabel lblHeartRate = new JLabel("Max Heart Rate:");
    lblHeartRate.setBounds(15, 362, 121, 20);
    contentPane.add(lblHeartRate);

    JLabel lblMsgBMI = new JLabel("");
    lblMsgBMI.setBounds(167, 301, 88, 20);
    contentPane.add(lblMsgBMI);

    JLabel lblCat = new JLabel("");
    lblCat.setBounds(167, 326, 69, 20);
    contentPane.add(lblCat);

    JLabel lblMsgHr = new JLabel("");
    lblMsgHr.setBounds(167, 362, 69, 20);
    contentPane.add(lblMsgHr);


}
private void calcFitness()
{
    HealthProfile hp = new HealthProfile();
    hp.setName(textFieldName.getText());
    hp.setAge(Integer.parseInt(textFieldAge.getText()));
    hp.setWeight(Double.parseDouble(textFieldWeight.getText()));
    hp.setHeight(Integer.parseInt(textFieldHeightFt.getText()), Integer.parseInt(textFieldHeightIn.getText()));
    double BMI = hp.getBMI();
    lblMsgBMI.setText(String.valueOf(BMI));
    String Category = hp.getCategory();
    lblMsgCat.setText(String.valueOf(Category));
    double hR = hp.getHR();
    lblMsgHr.setText(String.valueOf(hR));


}

}

This is the error: Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at iLab2.HealthProfileGUI.calcFitness(HealthProfileGUI.java:148) at iLab2.HealthProfileGUI.access$0(HealthProfileGUI.java:140) at iLab2.HealthProfileGUI$2.mouseClicked(HealthProfileGUI.java:104) at java.awt.AWTEventMulticaster.mouseClicked(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$500(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$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source) at java.security.ProtectionDomain$JavaSecurityAccessImpl.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$JavaSecurityAccessImpl.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)

Can someone help? Thank you