-4

I don't understand why I get the exception. Here is my code:

This it my HealthProfile class where I have the getters and setters.

    public class HealthProfile {

    //creating the variables
    private String name;
    private int age;
    private double weight;
    private double heightInches;
    private double bmi;

    // creating the constructor
    public HealthProfile() {


    }

    // setting up the SETS

    public void setName(String name1) {
        this.name = name1;
    }

    public void setAge(int age1) {
        this.age = age1;
    }

    public void setWeight(double weight1) {
        this.weight = weight1;
    }

    public void setHeight(double heightFeet, double heightInches) {

        this.heightInches = heightFeet * 12 + heightInches;

    }

    public void setBmi(double bmi) {
        this.bmi = bmi;
    }

    // setting up the GETS

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

    public double getWeight() {
        return weight;
    }

    public double getHeightInches() {
        return heightInches;
    }

    public double getBMI(double BMI) {

        bmi = (weight * 703) / (heightInches * heightInches);
        //rounding to one decimal
        return Math.round(bmi * 10.0) / 10.0;
    }

        // get the category
    public String getCategory(double bmi) {
        if (bmi < 18.5) {
            return "Underweight";
        } else if (bmi >= 18.5 && bmi < 25) {
            return "Normal weight";
        } else if (bmi >= 25 && bmi < 30) {
            return "Overweight";
        } else if (bmi >= 30) {
            return "Obese";
        } else {
            return "Unknown";
        }
    }
    //maximum heart rate
    public int getMaxHR(double getMaxHr) {
        return 220 - getAge();
    }

}

Here is my class that extends JFrame:

public class HealthProfileGUI extends JFrame {

private HealthProfile hp;
private JTextField txtName;
private JTextField txtAge;
private JTextField txtWeight;
private JTextField txtHeightF;
private JTextField txtHeighti;
private JTextField txtBmi;
private JTextField txtCategory;
private JTextField txtMaxHeartRate;
private JButton btnDisplay;
private JButton btnClear;

public HealthProfileGUI() {

    super("BMI Calculator");
    hp = new HealthProfile();

    getContentPane().setLayout(null);

    // LABEL NAME

    JLabel lblName = new JLabel("Name: ");
    lblName.setBounds(102, 11, 46, 14);
    getContentPane().add(lblName);

    // LABEL AGE
    JLabel lblAge = new JLabel("Age:");
    lblAge.setBounds(102, 51, 46, 14);
    getContentPane().add(lblAge);

    // LABEL WEIGHT
    JLabel lblWeight = new JLabel("Weight:");
    lblWeight.setBounds(102, 96, 46, 14);
    getContentPane().add(lblWeight);

    // LBL HEIGHT FEET
    JLabel lblHeightfeet = new JLabel("Height-feet:");
    lblHeightfeet.setBounds(102, 147, 83, 14);
    getContentPane().add(lblHeightfeet);

    // LABEL BMI
    JLabel lblBmi = new JLabel("BMI:");
    lblBmi.setBounds(102, 319, 46, 14);
    getContentPane().add(lblBmi);

    // LABEL CATEGORY
    JLabel lblCategory = new JLabel("Category:");
    lblCategory.setBounds(102, 368, 66, 14);
    getContentPane().add(lblCategory);

    // LABEL MAX HEART RATE
    JLabel lblMaxHeartRate = new JLabel("Max Heart Rate:");
    lblMaxHeartRate.setBounds(102, 418, 93, 14);
    getContentPane().add(lblMaxHeartRate);

    // LABEL HEIGHT INCHES
    JLabel lblHeightInches = new JLabel("Height-Inches:");
    lblHeightInches.setBounds(102, 195, 83, 14);
    getContentPane().add(lblHeightInches);

    // TEXT NAME
    txtName = new JTextField();
    txtName.setBounds(203, 8, 164, 20);
    getContentPane().add(txtName);
    txtName.setColumns(10);

    // TEXT AGE
    txtAge = new JTextField();
    txtAge.setBounds(203, 48, 164, 20);
    getContentPane().add(txtAge);
    txtAge.setColumns(10);

    // TEXT HEIGHT FEET
    txtHeightF = new JTextField();
    txtHeightF.setBounds(203, 144, 164, 20);
    getContentPane().add(txtHeightF);
    txtHeightF.setColumns(10);

    // TEXT BMI
    txtBmi = new JTextField();
    txtBmi.setBounds(199, 316, 168, 20);
    getContentPane().add(txtBmi);
    txtBmi.setColumns(10);

    // TEXT CATEGORY
    txtCategory = new JTextField();
    txtCategory.setBounds(199, 365, 168, 20);
    getContentPane().add(txtCategory);
    txtCategory.setColumns(10);

    // TEXT HEART RATE
    txtMaxHeartRate = new JTextField();
    txtMaxHeartRate.setBounds(199, 415, 168, 20);
    getContentPane().add(txtMaxHeartRate);
    txtMaxHeartRate.setColumns(10);

    // TEXT WEIGHT
    txtWeight = new JTextField();
    txtWeight.setBounds(203, 93, 164, 20);
    getContentPane().add(txtWeight);
    txtWeight.setColumns(10);

    // TEXT HEIGHT INCHES
    txtHeighti = new JTextField();
    txtHeighti.setBounds(203, 192, 164, 20);
    getContentPane().add(txtHeighti);
    txtHeighti.setColumns(10);

    // BUTTON DISPLAY
    btnDisplay = new JButton("Display");
    btnDisplay.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            hp.setName(txtName.getText());
            hp.setAge(Integer.parseInt(txtAge.getText()));
            hp.setWeight(Double.parseDouble(txtWeight.getText()));
            hp.setHeight(Double.parseDouble(txtHeightF.getText()),
                    Double.parseDouble(txtHeighti.getText()));

            hp.getBMI(Double.parseDouble(txtBmi.getText()));
            hp.getCategory(Double.parseDouble(txtCategory.getText()));
            hp.getMaxHR(Double.parseDouble(txtMaxHeartRate.getText()));

            if (txtName.equals("")) {
                JOptionPane.showMessageDialog(null, "Name cannot be blank!");
            }
            else if (txtAge.equals("")) {
                JOptionPane.showMessageDialog(null, "Age cannot be blank!");
            }

            else if (txtWeight.equals("")) {
                JOptionPane.showMessageDialog(null, "Weight cannot be blank!");
            }
            else if (txtHeightF.equals("")) {
                JOptionPane.showMessageDialog(null, "Height cannot be blank!");
            }
            else if (txtHeighti.equals("")) {
                JOptionPane.showMessageDialog(null, "Height cannot be blank!");
            }
        }
    });
    btnDisplay.setBounds(59, 250, 148, 31);
    getContentPane().add(btnDisplay);

    // BUTTON CLEAR
            btnClear = new JButton("Clear");
            btnClear.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent arg0) {
                    txtName.setText("");
                    txtAge.setText("");
                    txtWeight.setText("");
                    txtHeightF.setText("");
                    txtHeighti.setText("");
                    txtBmi.setText("");
                    txtCategory.setText("");
                    txtMaxHeartRate.setText("");

                }
            });
            btnClear.setBounds(251, 250, 148, 31);
            getContentPane().add(btnClear);
}

}

And here is the exception that I get:

Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: ""
    at java.lang.NumberFormatException.forInputString(Unknown Source)
    at java.lang.Integer.parseInt(Unknown Source)
    at java.lang.Integer.parseInt(Unknown Source)
    at com.samhorga.week2.HealthProfileGUI$1.actionPerformed(HealthProfileGUI.java:131)
    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$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)
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
SamH
  • 41
  • 1
  • 8
  • 2
    *java.lang.NumberFormatException: For input string: ""* what number do you think `""` should be ? – Scary Wombat Mar 08 '17 at 02:56
  • I saw that first line of the exception. But I can't figure it out. For each field that require numbers, I converted them in the btnDisplay, from String to int/double depending by requirement. – SamH Mar 08 '17 at 03:01
  • 2
    (1-) `But I can't figure it out.` - What can't you figure out? The message tells you the line causing the problem. Above that error message you see a reference to the `Integer.parseInt(...)` method. So the parameter you pass to the parseInt(...) method at that statement is an empty String. Every Exception you get will give you information like this you can use to solve the problem. – camickr Mar 08 '17 at 03:44
  • 1
    `getContentPane().setLayout(null);` Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). – Andrew Thompson Mar 08 '17 at 03:50
  • 1
    @SamH How can you convert `""` to a number? The field is empty – MadProgrammer Mar 08 '17 at 03:53

1 Answers1

1

In the actionPerformed method for butnDisplay you have:

btnDisplay.addActionListener(new ActionListener() {
  public void actionPerformed(ActionEvent arg0) {
      hp.setName(txtName.getText());
      hp.setAge(Integer.parseInt(txtAge.getText()));

The exception you see is because of the call to Integer.parseInt() with an empty string as the parameter. It is only after that call that you are checking for txtAge equal to "".

So instead move the hp.setAge() etc. calls to the point after the checks (and of course you'll need a 'return' for each of those error cases)

Oscar
  • 334
  • 1
  • 10