Could someone help me correct this error, thank you in advance. I have been trying to correct for a while but I am unsuccessful in my attempts.
I don't see an error in the code when registering it works well, it is redirected to main activity but when I go out and try to login, this error described below occurs.
Any help is welcome. I'm getting this error
java.lang.NullPointerException: Attempt to invoke virtual method 'void myapp.COM.model.Usuario.setEmail(java.lang.String)' on a null object reference
at myapp.COM.activity.LoginActivity$1.onClick(LoginActivity.java:54)
Login code:
//Fazer login do usuario
progressBar.setVisibility( View.GONE );
botaoEntrar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String textoEmail = campoEmail.getText().toString();
String textosenha = campoSenha.getText().toString();
if( !textoEmail.isEmpty() ){
if( !textosenha.isEmpty() ){
usuario.setEmail( textoEmail );
usuario.setSenha( textosenha );
validarLogin( usuario );
}else{
Toast.makeText(LoginActivity.this,
"Preencha a senha!",
Toast.LENGTH_SHORT).show();
}
}else{
Toast.makeText(LoginActivity.this,
"Preencha o e-mail!",
Toast.LENGTH_SHORT).show();
}
}
});
Model Usuario
public class Usuario implements Serializable {
private String id;
private String nome;
private String email;
private String senha;
private String caminhoFoto;
private int seguidores = 0;
private int seguindo = 0;
private int postagens = 0;
private String money;
private String cassinotime;
private String cassinoprofit;
public String getMoney() {
return money;
}
public void setMoney(String money) {
this.money = money;
}
public String getCassinotime() {
return cassinotime;
}
public void setCassinotime(String cassinotime) {
this.cassinotime = cassinotime;
}
public String getCassinoprofit() {
return cassinoprofit;
}
public void setCassinoprofit(String cassinoprofit) {
this.cassinoprofit = cassinoprofit;
}
public Usuario() {
}
public void salvar(){
DatabaseReference firebaseRef = ConfiguracaoFirebase.getFirebase();
DatabaseReference usuariosRef = firebaseRef.child("usuarios").child( getId() );
usuariosRef.setValue( this );
}
public void atualizarQtdPostagem(){
DatabaseReference firebaseRef = ConfiguracaoFirebase.getFirebase();
DatabaseReference usuariosRef = firebaseRef
.child("usuarios")
.child( getId() );
HashMap<String, Object> dados = new HashMap<>();
dados.put("postagens", getPostagens() );
usuariosRef.updateChildren( dados );
}
public void atualizar(){
DatabaseReference firebaseRef = ConfiguracaoFirebase.getFirebase();
Map objeto = new HashMap();
objeto.put("/usuarios/" + getId() + "/nome", getNome() );
objeto.put("/usuarios/" + getId() + "/caminhoFoto", getCaminhoFoto() );
firebaseRef.updateChildren( objeto );
}
public Map<String, Object> converterParaMap(){
HashMap<String, Object> usuarioMap = new HashMap<>();
usuarioMap.put("email", getEmail() );
usuarioMap.put("nome", getNome() );
usuarioMap.put("id", getId() );
usuarioMap.put("caminhoFoto", getCaminhoFoto() );
usuarioMap.put("seguidores", getSeguidores() );
usuarioMap.put("seguindo", getSeguindo() );
usuarioMap.put("postagens", getPostagens() );
return usuarioMap;
}
public int getSeguidores() {
return seguidores;
}
public void setSeguidores(int seguidores) {
this.seguidores = seguidores;
}
public int getSeguindo() {
return seguindo;
}
public void setSeguindo(int seguindo) {
this.seguindo = seguindo;
}
public int getPostagens() {
return postagens;
}
public void setPostagens(int postagens) {
this.postagens = postagens;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome.toUpperCase();
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Exclude
public String getSenha() {
return senha;
}
public void setSenha(String senha) {
this.senha = senha;
}
public String getCaminhoFoto() {
return caminhoFoto;
}
public void setCaminhoFoto(String caminhoFoto) {
this.caminhoFoto = caminhoFoto;
}
}
You need to create the object of Usuario
class before setting attributes
to the object of Usuario
.
Please try below:
botaoEntrar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Usuario usuario = new Usuario();
String textoEmail = campoEmail.getText().toString();
String textosenha = campoSenha.getText().toString();
if( !textoEmail.isEmpty() ){
if( !textosenha.isEmpty() ){
usuario.setEmail( textoEmail );
usuario.setSenha( textosenha );
validarLogin( usuario );
}else{
Toast.makeText(LoginActivity.this,
"Preencha a senha!",
Toast.LENGTH_SHORT).show();
}
}else{
Toast.makeText(LoginActivity.this,
"Preencha o e-mail!",
Toast.LENGTH_SHORT).show();
}
}
});