Reputation: 2652
I just started programming in java and i am creating a simple waiting list. it al seems to work well but i decided to include a if else construction to check the textfield not beeing empty. the problem is that it seems to be ignored because i don't get a error or something.. and i googled alot for the if else example and i can't solve the problem somehow.. what am i doing wrong? below you can find the relevant code. Thanks in advance.
public void actionPerformed(ActionEvent e) {
// check if veld1 is filled in.
if ( veld1 == null || veld1.equals( "" ) ) {
// give error
System.out.println("U heeft niets ingevuld in veld1");
}
else {
veld4.setText( veld3.getText() );
veld3.setText( veld2.getText() );
veld2.setText( veld1.getText() );
textveld1.append( veld4.getText() + "\n" );
veld1.setText("");
}
}
Upvotes: 0
Views: 96
Reputation: 5066
If the veld1
holds a JTextField, you probably want to change the statement to veld1 == null || veld1.getText() == null || veld1.getText().equals( "" )
, as in your current code you check if the field itself exists, not its content.
Upvotes: 1
Reputation: 24885
It is difficult to grant without seeing the rest of it, but veld1.equals("")
looks suspicious. You are comparing veld1
to the empty String, but veld1
looks like a component. Maybe you meant veld1.getText().equals("")
(and, similarly, veld1.getText() == null
)
Upvotes: 1
Reputation: 111
veld1.equals("") is not the same as veld1.getText().equals(""), the first one is comparing the veld1 object to an empty string, and will always be false.
Upvotes: 0
Reputation: 229098
It seems veld1 is not a string, but some Swing control. You probably want to do
if(veld1.getText() == null || veld1.getText().equals( "" )
Upvotes: 8