Error making calling methods from one class through another

Asked

Viewed 32 times

0

My application crashes when I write this code below, I am trying to stop the Login Class in the Registration Class and access the Login class methods through the Registration class.

    ca.getLogar().setLogin(logCad.getText().toString());
    ca.getLogar().setSenha(senhaCad.getText().toString());

Does anyone have any idea how to solve this ? These are the access method and attributes in the login class

private LoginClasse logar;

    public LoginClasse getLogar() {
        return logar;
    }

    public void setLogar(LoginClasse logar) {
        this.logar = logar;
    }

Login class

    public class LoginClasse {
    
        private String login;
        private String senha;
    
    
    
        public String getLogin() {
            return login;
        }
    
        public void setLogin(String login) {
            this.login = login;
        }
    
        public String getSenha() {
            return senha;
        }
    
        public void setSenha(String senha) {
            this.senha = senha;
        }
    }

Register Class

public class CadastroClasse {

    private String nome;
    private String cpf;
    private String tel;
    private String end;
    private String cep;
    private String email;

    private LoginClasse logar;

    public LoginClasse getLogar() {
        return logar;
    }

    public void setLogar(LoginClasse logar) {
        this.logar = logar;
    }

    public String getNome() {
        return nome;
    }

    public void setNome(String nome) {
        this.nome = nome;
    }

    public String getCpf() {
        return cpf;
    }

    public void setCpf(String cpf) {
        this.cpf = cpf;
    }

    public String getTel() {
        return tel;
    }

    public void setTel(String tel) {
        this.tel = tel;
    }

    public String getEnd() {
        return end;
    }

    public void setEnd(String end) {
        this.end = end;
    }

    public String getCep() {
        return cep;
    }

    public void setCep(String cep) {
        this.cep = cep;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }
}

      

Registration method that is responsible for the problem

    public void Cadastrar(View view)
        {
            CadastroClasse ca=new CadastroClasse();
        
    
            ca.getLogar().setLogin(logCad.getText().toString());
            ca.getLogar().setSenha(senhaCad.getText().toString());
            ca.setNome(nome.getText().toString());
            ca.setCpf(cpf.getText().toString());
            ca.setTel(tel.getText().toString());
            ca.setEnd(end.getText().toString());
            ca.setCep(cep.getText().toString());
            ca.setEmail(email.getText().toString());
    
            startActivity(new Intent(Cadastro.this,MainActivity.class));
    
        }

1 answer

0


Your Registration class has the private Loginclasse attribute log in, but you do not initialize it at any time. With this, you are trying to call setLogin a variable that has not been initialized, and this ends up generating an error.

To solve this you could, modify your main and call setLog the register class by passing a Loginclasse object, example:

public void Cadastrar(View view)
        {
            CadastroClasse ca=new CadastroClasse();
        
            ca.setLogar(LoginClasse());//linha adicionada por mim
            ca.getLogar().setLogin(logCad.getText().toString());
            ca.getLogar().setSenha(senhaCad.getText().toString());
            ca.setNome(nome.getText().toString());
            ca.setCpf(cpf.getText().toString());
            ca.setTel(tel.getText().toString());
            ca.setEnd(end.getText().toString());
            ca.setCep(cep.getText().toString());
            ca.setEmail(email.getText().toString());
    
            startActivity(new Intent(Cadastro.this,MainActivity.class));
    
        }

Another way would be to instantiate this class in the constructor of the Register class, example

public class CadastroClasse {

    private String nome;
    private String cpf;
    private String tel;
    private String end;
    private String cep;
    private String email;

    private LoginClasse logar;
    
    public CadastroClasse(){
        logar = LoginClasse()
    }
   
    //resto do codigo

Doing one of these ways you will be initiating the log variable and will be able to access the Loginclasse class methods.

Browser other questions tagged

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