I have a mistake I can’t identify in the println

Asked

Viewed 76 times

3

These are my classes but still the error occurs:

Exception in thread "main" java.lang.Nullpointerexception funcio20.Principal.main(Main.java:16) /home/Lucas/. cache/netbeans/8.1/executor-snippets/run.xml:53: Java returned: 1 BUILD FAILED (total time: 0 Seconds) Follows my code:

Classe Principal:

public class Principal {

    public static void main(String[] args) {

        Funcionario lucas = new Funcionario();
        lucas.setSalario(1856.85);
        lucas.setEndereco("Rua Frei Caneca");
        lucas.setEstadoCivil("Solteiro");
        lucas.setNomeDoDepartamento("Auxiliar");

        lucas.pessoa = new Pessoa();
        lucas.pessoa.setNome("Lucas Alves Cardoso de Jesus");
        lucas.pessoa.setRg(123456891);

        lucas.data.setDatadia(13);
        lucas.data.setDatames(05);
        lucas.data.setDiaano(94);

        lucas.getAnual();

        System.out.println(lucas.getEndereco());

    }

}

Staff Class

public class Funcionario {

    private String NomeDoDepartamento;
    private double Salario;
    private double anual;
    private String EstadoCivil;
    private String Endereco;
    int proximo = 0;
    Pessoa pessoa;
    Data data;


    //get obter set mudar

    public String getNomeDoDepartamento(){
        return this.NomeDoDepartamento;
    }

    public void setNomeDoDepartamento(String nomedepartamento){
        this.NomeDoDepartamento = nomedepartamento;
    }

    public double getSalario(){
        return this.Salario;
    }

    public void setSalario (double salario){
        this.Salario = salario;
    }

    public double getAnual(){
        return this.Salario*12;
    }

    public String getEstadoCivil(){
        return this.EstadoCivil;
    }

    public void setEstadoCivil(String estadocivil){
        this.EstadoCivil = estadocivil;
    }

    public String getEndereco(){
        return this.Endereco;
    }

    public void setEndereco (String endereco){
        this.Endereco = endereco;
    }


}
  • Your Problem is not in println! your class funcionário contains some errors. vc did not set a setDatadia(), setDatames(), setDiaAno(). in the main class you forgot to set properties to data

2 answers

1


You didn’t set the property data before line 16 of its main class:

lucas.data = new Data();
lucas.data.setDatadia(13);

Other than that I have at least 2 tips to give you:

1) Use the constructor to set new objects or create auxiliary constructors:

public Funcionario() {
  this.pessoa = new Pessoa();
  this.data = new Data();
}

OR

public Funcionario(){} // construtor padrão

public Funcionario(Pessoa pessoa) {
  this.pessoa = pessoa;
}
public Funcionario(Data data) {
  this.data = data;
}
public Funcionario(Pessoa pessoa, Data data) {
  this.pessoa = pessoa;
  this.data = data;
}

Thus allowing:

Funcionario funcionario = new Funcionario(); 
// funcionário padrão, sem Pessoa e Data

Funcionario funcionario = new Funcionario(new Pessoa()); 
// funcionário com Pessoa e sem Data

Funcionario funcionario = new Funcionario(new Data()); 
// funcionário sem Pessoa e com Data

Funcionario funcionario = new Funcionario(new Pessoa(), new Data); 
// funcionário com Pessoa e Data

2) Do not use direct access to these properties, always use setters and getters:

public Pessoa getPessoa() {
  return this.pessoa;
}

public void setPessoa(Pessoa pessoa) {
  this.pessoa = pessoa;
}
  • 1

    @Giovane although the author approved the edition, you can not do this type of editing on the site, should not change or add content in the post, in order to change the original content of the post, as you did adding a "response attempt". Visit here to read more about what can be changed in the site posts.

  • @diegofm I have not added anything to the question. I thought Sorack’s answer was good and I didn’t want to add an answer because I just wanted to complement his answer because I thought it was valid to present such a code strategy. I thought it would be worse if I made an answer like Sorack’s or an answer wanting to complete his answer.

  • 1

    @Giovane this is not allowed on the site, if your reply adds to his with new content, it is highly advisable you reply in a new post. The link explains exactly that.

  • 3

    @Giovane vc could publish a new reply and quote the Sorack reply in his reply, or make a reference, usually this type of edition is not recommended on the site, can change the direction of the answer.

0

Your problem is not in println! Your employee class contains some errors. You have not defined a setDatadia(), setDatames(), setDiaAno(). In the main class you forgot to define the data.

Main class:

package principal;


public class Principal {


public static void main(String[] args) {

    funcionario lucas = new funcionario();
    lucas.setSalario(1856.85);
    lucas.setEndereco("Rua Frei Caneca");
    lucas.setEstadoCivil("Solteiro");
    lucas.setNomeDoDepartamento("Auxiliar");

   lucas.pessoa = new funcionario();
    lucas.pessoa.setNome("Lucas Alves Cardoso de Jesus");
    lucas.pessoa.setRg(123456891);

    lucas.data = new funcionario();
    lucas.data.setDataDia(13);
    lucas.data.setDataMes(05);
    lucas.data.setDiaAno(94);

    lucas.getAnual();

    System.out.println(lucas.getEndereco());


}

}

Working class:

package principal;


public class funcionario {

    private String NomeDoDepartamento;
    private double Salario;
    private double anual;
    private String EstadoCivil;
    private String Endereco;
    private String nome;
    private int rg;
    int proximo = 0;
    private int dataDia;
    private int dataMes;
    private int  diaAno;

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

    public void setRg(int rg) {
        this.rg = rg;
    }

    public void setDataDia(int dataDia) {
        this.dataDia = dataDia;
    }

    public void setDataMes(int dataMes) {
        this.dataMes = dataMes;
    }

    public void setDiaAno(int diaAno) {
        this.diaAno = diaAno;
    }
    funcionario pessoa;
    funcionario data;


    //get obter set mudar

    public String getNomeDoDepartamento(){
        return this.NomeDoDepartamento;
    }

    public void setNomeDoDepartamento(String nomedepartamento){
        this.NomeDoDepartamento = nomedepartamento;
    }

    public double getSalario(){
        return this.Salario;
    }

    public void setSalario (double salario){
        this.Salario = salario;
    }

    public double getAnual(){
        return this.Salario*12;
    }

    public String getEstadoCivil(){
        return this.EstadoCivil;
    }

    public void setEstadoCivil(String estadocivil){
        this.EstadoCivil = estadocivil;
    }

    public String getEndereco(){
        return this.Endereco;
    }

    public void setEndereco (String endereco){
        this.Endereco = endereco;
    }


}
  • I unified your two answers into one. So I marked the other one for exclusion. I hope you understand, because the two answers you posted are actually one thing.

Browser other questions tagged

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