"java.lang.NullPointerException" error in Java

2

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

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

To send the notes I wrote this:     // Na main     aluno.setAvaliacao (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 Rating constructor:

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

The get for the notes looks like this:

public Avaliacao getAvaliacao() {
    return avaliacao;
}

And the

private Avaliacao avaliacao;

My question is why is the error appearing when I'm going to print any note on main? Here's how I did the logic to show some 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 class Student:

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;
}
}

Follow the Class App:

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;
}
}
    
asked by anonymous 20.03.2017 / 18:54

2 answers

5

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

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

Aluno aluno = new Aluno ();
aluno.setAvalicao(18,17,12,9);
    
20.03.2017 / 19:02
1
Aluno aluno = new Aluno ();
//Falta setar as notas do aluno
aluno.setAvalicao(...)
Avaliacao poo1 = aluno.getAvaliacao();
    
20.03.2017 / 18:58