2017-03-08 1 views
-4

Je ne comprends pas pourquoi j'obtiens l'exception. Voici mon code:Exception dans le fil "AWT-EventQueue-0" NumberFormatException

Ce qu'il ma HealthProfile classe où je les accesseurs.

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

} 

Voici ma classe qui étend 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); 
} 

} 

Et voici l'exception que je reçois:

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 : Pour la chaîne d'entrée: "" * quel numéro pensez-vous que "" "' devrait être? –

+0

J'ai vu cette première ligne de l'exception. Mais je ne peux pas le comprendre. Pour chaque champ qui nécessite des nombres, je les ai convertis dans le btnDisplay, de String à int/double en fonction des besoins. – SamH

+2

(1-) 'Mais je ne peux pas le comprendre.» - Qu'est-ce que tu ne peux pas comprendre? Le message vous indique la ligne à l'origine du problème. Au-dessus de ce message d'erreur, vous voyez une référence à la méthode 'Integer.parseInt (...)'. Donc, le paramètre que vous passez à la méthode parseInt (...) à cette instruction est une chaîne vide. Chaque exception que vous obtenez vous donnera des informations comme celle-ci que vous pouvez utiliser pour résoudre le problème. – camickr

Répondre

1

Dans la méthode actionPerformed pour butnDisplay vous:

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

L'exception que vous voyez est due à l'appel à Integer.parseInt() avec une chaîne vide comme paramètre. Ce n'est qu'après cet appel que vous vérifiez que txtAge est égal à "".

déplacer Ainsi, au lieu de la hp.setAge(), etc. appelle au point après les contrôles (et bien sûr, vous aurez besoin d'un « retour » pour chacun de ces cas d'erreur)