线程“AWT-EventQueue-0”NumberFormatException中的异常

时间:2017-03-08 02:52:28

标签: java swing numberformatexception

我不明白为什么我会得到例外。这是我的代码:

这是我的HealthProfile课,我有吸气剂和制定者。

    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();
    }

}

这是我的课程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);
}

}

以下是我得到的例外情况:

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)

1 个答案:

答案 0 :(得分:1)

在butnDisplay的actionPerformed方法中,你有:

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

您看到的异常是因为调用Integer.parseInt()并将空字符串作为参数。只有在那次通话之后,你才检查txtAge是否等于&#34;&#34;。

因此,请将hp.setAge()等调用移至检查后的点(当然,对于每个错误情况,您都需要返回&#39;)