2017-03-08 101 views
-4

我不明白爲什麼我會得到異常。這是我的代碼:線程「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

回答

1

在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()等調用到檢查後點(當然,你需要爲每個那些錯誤情況下,「迴歸」)