Search code examples
javaswingawtjtextfield

Java JTextField empty string error


I keep getting an error for empty string as you can see below

Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: ""

I am unsure why I keep getting this error since I do not try to calculate without typing a number within the JTextField. Below is the code I am using for the calculate button which is fairly simple.

private void ButtonCalculateActionPerformed(java.awt.event.ActionEvent evt) {                                                
         Height = Integer.parseInt(this.TextFieldHeight.getText());
         Weight = Integer.parseInt(this.TextFieldWeight.getText());
         BMI = Integer.parseInt(this.TextFieldBMI.getText());
         answer = Height + Weight;
        this.TextFieldBMI.setText("The answer is: " + answer);
    }                                               

These are the defined variables

//variables
    static int Height;
    static int Weight;
    static int BMI;
    static int answer;

Solution

  • Sorry I apologize! I added the BMI variable when I shouldn't have since an answer is suppose to be outputted into the TextFieldBMI rather than .getText() from it so it was always empty. Problem solved!