"java.lang.Nullpointerexception" error in Java

Asked

Viewed 871 times

2

Good afternoon, I have an error on line 17 of main. I have a "Student" object and another "POO1" object. I have written the following code:

Aluno aluno = new Aluno ();
Avaliacao poo1 = aluno.getAvaliacao();

To send the notes I wrote this: //Na main setAvailability(2.3, 3.1, 5.2, 1.1);

//Na classe Aluno
public void setAvaliacao(double a, double b, double c, double d) {
    Avaliacao x = new Avaliacao (a, b, c, d);
    avaliacao = x;
}

And the Appraisal Builder:

 public Avaliacao(double nota1, double nota2, double exercicioAula, double trabalhofinal) {
    this.nota1 = nota1;
    this.nota2 = nota2;
    this.exercicioAula = exercicioAula;
    this.trabalhoFinal = trabalhofinal;
}

The get to the notes stayed that way:

public Avaliacao getAvaliacao() {
    return avaliacao;
}

And the attribute

private Avaliacao avaliacao;

My question is why is the error appearing when I will print any note in main? It follows how I did the logic to show any note sent:

System.out.println("Nota 1: " + poo1.getTrabalhoFinal()); //Aqui aparece erro
//Erro: Exception in thread "main" java.lang.NullPointerException

If in doubt, follow the Student class:

public class Aluno {
private String nome;
private String matricula;
private Avaliacao avaliacao;

public Aluno() {
}

public String getNome() {
    return nome;
}

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

public String getMatricula() {
    return matricula;
}

public void setMatricula(String matricula) {
    this.matricula = matricula;
}

public Avaliacao getAvaliacao() {
    return avaliacao;
}

public void setAvaliacao(double a, double b, double c, double d) {
    Avaliacao x = new Avaliacao (a, b, c, d);
    avaliacao = x;
}
}

Follows the class Evaluation:

public class Avaliacao {
private double nota1;
private double nota2;
private double exercicioAula;
private double trabalhoFinal;

public Avaliacao(double nota1, double nota2, double exercicioAula, double trabalhofinal) {
    this.nota1 = nota1;
    this.nota2 = nota2;
    this.exercicioAula = exercicioAula;
    this.trabalhoFinal = trabalhofinal;
}

public double getNota1() {
    return nota1;
}

public void setNota1(double nota1) {
    this.nota1 = nota1;
}

public double getNota2() {
    return nota2;
}

public void setNota2(double nota2) {
    this.nota2 = nota2;
}

public double getExercicioAula() {
    return exercicioAula;
}

public void setExercicioAula(double exercicioAula) {
    this.exercicioAula = exercicioAula;
}

public double getTrabalhoFinal() {
    return trabalhoFinal;
}

public void setTrabalhoFinal(double trabalhoFinal) {
    this.trabalhoFinal = trabalhoFinal;
}
}
  • I think the problem is happening because you didn’t call the method setAvaliacao before calling the getAvaliacao

  • and its main class?

  • The mistake was exactly that, the set was after the get, I was thinking otherwise. Thank you.

  • Okay, I’m going to test it here. My teacher did it this way in class.

  • How can I leave the set?

2 answers

5


You’re not even showing what makes the getTrabalhoFinal() method. To be giving this error is because the instances you want to show are not being created, that is, the values returned by the getTrabalhoFinal() method are null. It is a matter of reviewing this case or showing all classes that influence the code flow for this case so that better help is possible.

To create the instances would have to do something like this, for example:

Aluno aluno = new Aluno ();
aluno.setAvalicao(18,17,12,9);
  • I understood my mistake, thank you very much!

  • Don’t you want to upvote there? eheh

1

Aluno aluno = new Aluno ();
//Falta setar as notas do aluno
aluno.setAvalicao(...)
Avaliacao poo1 = aluno.getAvaliacao();

Browser other questions tagged

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