The Operator != is Undefined for the argument type(s)

Asked

Viewed 1,190 times

4

I am trying to create a method to save users in the database by doing the following check if the id past user is different from null he changes if not he registered.

Code

    public void salvar(Usuario usuario){
    if(usuario.getIdUsuario() != null){ //erro aqui
        aleterar(usuario);
    }else {
        cadastrar(usuario);
    }
} 

Error

The Operator != is Undefined for the argument type(s) type int, null

  • The Operator != is Undefined for the argument type(s) type int, null

2 answers

7

Primitive types cannot assume value null, only objects

if(usuario.getIdUsuario() != 0){
    aleterar(usuario);
}else {
    cadastrar(usuario);
}
  • I had forgotten this detail , Birgado.

6


If you need to check if the object is null, just check it, if you need to check the result, then the check should be another, depends on the return of the method used:

public void salvar(Usuario usuario) {
    if (usuario != null) alterar(usuario);
    else cadastrar(usuario);
}

I put in the Github for future reference.

  • That’s right, in case I will register the user in the bank, if it is null, I register if it is not understood that the user wants to claim.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.