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

问题描述:

我不明白为什么我会得到异常。这是我的代码:线程“AWT-EventQueue-0”中的异常NumberFormatException

这是我的HealthProfile类,我有getters和setter。

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) 
+2

* java.lang.NumberFormatException :对于输入字符串:“”*你认为应该是什么数字? –

+0

我看到了异常的第一行。但我无法弄清楚。对于每个需要数字的字段,我根据需要将它们在btnDisplay中进行转换,从String转换为int/double。 – SamH

+2

(1-)'但我无法弄清楚。' - 你弄不清楚什么?该消息告诉你导致问题的线路。在该错误消息之上,您会看到对“Integer.parseInt(...)”方法的引用。所以你传递给parseInt(...)方法的参数是一个空字符串。您获得的每个例外都会为您提供可用于解决问题的信息。 – camickr

在actionPerformed方法butnDisplay您有:

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

您看到的异常是因为使用空字符串作为参数调用Integer.parseInt()。只有在那个调用之后,你才会检查txtAge等于“”。

因此,而不是移动hp.setAge()等调用到检查后点(当然,你需要为每个那些错误情况下,“回归”)