2
On my system, I have a window and a controller, but for some reason, it is never launching the error as expected :
Method of validation :
private void validacao(){
if((form.txtNome.getText() != null) && (form.txtEnd.getText() != null) && (form.txtTel.getText() != null)){
eventosForm();
}else{
JOptionPane.showMessageDialog(null, "Todos os campos devem estar preenchidos !");
}
}
Method that takes the values of the fields :
private void eventosForm(){
form.btnCadastrar.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Cliente cliente = new Cliente();
cliente.setNome(form.txtNome.getText());
cliente.setEndereco(form.txtEnd.getText());
cliente.setTelefone(form.txtTel.getText());
clienteDB.add(cliente);
form.setVisible(false);
}
});
}
As I said before, even if I leave all fields null, it performs the registration, I believe it may be because it is a void method, but I’m not sure ...
My intention is just to call the method
eventosForm();
if none field is empty and null, so the logic would not replace allor
forand
?– Daniel Santos
@Danielsantos not, because a field can be null or empty, are different states. A string is empty when its size is 0, and null when it has no value in it. But between the pairs of validations in each field, you should use the
&&
.– user28595
Still registering, even if I put the
&&
between pairs of each field ...– Daniel Santos
@Danielsantos forgive me the misconception, the code really was having problems in boolean logic, had done without testing before. Now it is correct.
– user28595
It is necessary to use Trim() as white spaces cause isEmpty() to return false.
– hlucasfranca
@hlucasfranca well remembered, thanks for the warning :)
– user28595
@Danielsantos if the answer answered, it would be interesting to mark it as accepted, thus, serve as reference for other users :)
– user28595