1

Im developing a small app and I wants to get the total when I click the button total . But if there are null values the code code dosen't work .So additionally following code was added .

int QtyOfChickenBurger;

if ((textField.getText().equals(null))) {
    QtyOfChickenBurger = Integer.parseInt(textField.getText())*0;
} else {
    QtyOfChickenBurger = Integer.parseInt(textField.getText()) * 70;
}

But still my application don't output the total when the textField is empty. So please help me to fix this.This is the full code.

JButton bttotal = new JButton("Total");
bttotal.setBounds(21, 37, 145, 25);
bttotal.setFont(new Font("Thoma", Font.PLAIN, 12));
bttotal.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg) {

            int QtyOfChickenBurger;

            if ((textField.getText().equals(null))) {

                QtyOfChickenBurger = Integer.parseInt(textField.getText())*0;
            } else {
                QtyOfChickenBurger = Integer.parseInt(textField.getText()) * 70;
            }

            int QtyOfChickenBurgerMeal = Integer.parseInt(textField_2.getText()) * 120;
            int QtyOfCheeseBurger = Integer.parseInt(textField_3.getText()) * 340;
            int QtyOfDrinks = Integer.parseInt(enterQTY.getText());
            int spriteCost = Integer.parseInt(enterQTY.getText()) * 55;
            int cokaColaCost = Integer.parseInt(enterQTY.getText()) * 60;
            int pepsiCost = Integer.parseInt(enterQTY.getText()) * 40;
            int lemonJuceCost = Integer.parseInt(enterQTY.getText()) * 35;
            int sum = (QtyOfChickenBurger + QtyOfChickenBurgerMeal + QtyOfCheeseBurger);
            lblDisplayCostOfMeals.setText(Integer.toString(sum));
            String x = String.valueOf(comboBox.getSelectedItem());

            if (x == "Sprite") {

                lblDisplayCostOfDrinks.setText(Integer.toString(spriteCost));
            } else if (x == "Select the drink") {
                lblDisplayCostOfDrinks.setText("0");
            } else if (x == "Coka Cola") {
                lblDisplayCostOfDrinks.setText(Integer.toString(cokaColaCost));
            } else if (x == "Pepsi") {
                lblDisplayCostOfDrinks.setText(Integer.toString(pepsiCost));
            } else if (x == "Lemon juce") {
                lblDisplayCostOfDrinks.setText(Integer.toString(lemonJuceCost));
            }
        }
    });
Pramod S. Nikam
  • 4,271
  • 4
  • 38
  • 62

5 Answers5

0

I presume you are using Swing JTextField which is a JTextComponent.

getText is returning a String therefore, you will need to check if its null or empty, before parsing it to int later on.

There are few ways of checking it, !textField.getText().isEmpty() and textField.getText() != null


Personally, i would use Commons Lang library StringUtils and just check if the string isBlank(textField.getText())

Additionally you should validate the input, as well You can use the mentioned library and use StringUtils.isNumeric() in your if statement.

LazerBanana
  • 6,865
  • 3
  • 28
  • 47
0

This code is wrong

if ((textField.getText().equals(null))) {    
    QtyOfChickenBurger = Integer.parseInt(textField.getText())*0;
}

if the textField.getText() is null then you can not parse the null into Integer. In addition, you multiply 0 with a number for what? => just set it to 0. Change it to

if (textField.getText()==null || textField.getText().equals("")){
    QtyOfChickenBurger = 0;
}
TuyenNTA
  • 1,194
  • 1
  • 11
  • 19
0

First of all textField.getText().equals(null) will never work this only throws a NullPointerException better use textField.getText()==null.

Because the user can enter anything in the TextField you need to validate the input or create a try-catch-block.

The best way would to create a help method to parse the numbers, for example:

public static int readInteger(String text, int defaultValue) {
    if(text == null)
        return defaultValue;
    try {
        return Integer.parseInt(text);
    } catch (NumberFormatException nfe) {
        return defaultValue;
    }
}

By the way don't compare Strings with x == "Pepsi" only when you want to check if the String is null.
Read: How do I compare strings in Java?

0

I am assuming that user can write what he want in that textfield so u should also take care when textfield have a value like 'asd';

  String QtyOfChickenBurgerAsString =textField.getText();
  Integer QtyOfChickenBurger=null;
  try{
  QtyOfChickenBurger = Integer.valueOf(QtyOfChickenBurgerAsString);
  }catch(Exception ex){
    System.out.println(" this is not a number ...do whatever u wanna do");
  }

  if (QtyOfChickenBurger!=null){
    System.out.println("integer value of field text  "+QtyOfChickenBurger);
  }

I suggest create variables with starting letter in lower case ,and when you compare using equals use the constant first.Also you could find a better solution by using components which accept only numbers in their text.

Uta Alexandru
  • 324
  • 4
  • 11
0

As of java 9, the Object class has a convenience method for handling null values and it is found in java.util.

public static String processEmployee(String fullName){
    // if fullName is null then second argument is returned,otherwise fullName is
    String name = Objects.requireNonNullElse(fullName,  "empty");
    // the processing goes here ...
    return name;
}
Pepe Alvarez
  • 1,218
  • 1
  • 9
  • 15