Moses Kirathe
Moses Kirathe

Reputation: 332

JTextfield.getText().equals() method not working

I am trying to check whether a jTextfield is empty using the following code, then display an error dialog if it is.

if(sproductname.getText().equals("")){
 JOptionPane.showMessageDialog(null,"One         or more fields is empty","Empty       field",JOptionPane.ERROR_MESSAGE);
     }

The method is in actionperformed and should be executed when I press a button but in this case nothing happens. What could be wrong is such a small block of code?

Upvotes: 0

Views: 701

Answers (2)

djdecks93
djdecks93

Reputation: 5

Ur code didn't working because basic value for object type String is null. Not empty string. All object type in Java have basic value == null.

Upvotes: 0

Jesús Castillo
Jesús Castillo

Reputation: 69

Try this:

if (sproductname.getText().isEmpty()){
    ...
}

Upvotes: 2

Related Questions