The operator! = is undefined for the argument type (s)

4

I am trying to create a method to save users in the database by doing the following check if the id of the last user is different from null it changes if it does not register.

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

    
asked by anonymous 28.03.2016 / 20:48

2 answers

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, it depends on the method return used:

public void salvar(Usuario usuario) {
    if (usuario != null) {
        alterar(usuario);
    } else {
        cadastrar(usuario);
    }
}
    
28.03.2016 / 21:08
7

Primitive types can not assume the value null , only objects

if(usuario.getIdUsuario() != 0){
    aleterar(usuario);
}else {
    cadastrar(usuario);
}
    
28.03.2016 / 21:05