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 thegetAvaliacao
– Artur Trapp
and its main class?
– user28595
The mistake was exactly that, the set was after the get, I was thinking otherwise. Thank you.
– Ygor Fraga
Okay, I’m going to test it here. My teacher did it this way in class.
– Ygor Fraga
How can I leave the set?
– Ygor Fraga