How to use arrays in Java?

5

It's a very early question (I'm new to Java, I came from C ++). I have to make a program that takes 4 notes of 10 students, but when I play with the "pointers" (arrays), my program returns an exception:

import java.util.Scanner;
class Aluno
{
    public int[] nota;
}

class Nota
{
    private Aluno[] aluno;
    private int totalAlunos;
    private int NumeroNotas;
    public Nota(int numero_alunos, int numero_notas)
    {
        this.totalAlunos = numero_alunos;
        this.NumeroNotas = numero_notas;
        this.aluno = new Aluno[numero_alunos];
        for(int i = 0; i < totalAlunos; i++)
        {
            this.aluno = new Aluno[numero_notas];
        }
        for(int numero_de_alunos = 0; numero_de_alunos < numero_alunos; numero_de_alunos++)
        {
            System.out.print("Aluno " + (numero_de_alunos+1) + ":\n");
            Scanner Scan = new Scanner(System.in);
            for(int i = 0; i < NumeroNotas; i++)
            {
                System.out.print("Digite a nota número " + (i+1) + ": ");
                this.aluno[numero_de_alunos].nota[i] = Scan.nextInt();
                System.out.print("\n");
            }
        }
    }
    public int PegarMedia(int NumeroAluno)
    {
        int stack = 0;
        for(int i = 0; i < NumeroNotas; i++)
        {
            stack += this.aluno[NumeroAluno].nota[i];
        }
        return stack / NumeroNotas;

    }
    public void MostrarMedia()
    {
        for(int i = 0; i < totalAlunos; i++)
        {
            System.out.print("Media do aluno " + i + ":");
            System.out.print(PegarMedia(i) + "\n");
        }
     System.out.print("\n");
    }
}
//Fim das classes

class Test
{
    public static void main(String args[])
    {
        Nota notas = new Nota(10,4);
        notas.MostrarMedia();
    }
}

Returns:

  

run: Student 1: Exception in thread "main"   java.lang.NullPointerException Type note number 1: at   Note: (Test.java:28) at Test.main (Test.java:59) Java Result: 1

What am I doing wrong? What should I do?

    
asked by anonymous 08.02.2014 / 15:03

2 answers

6

Instantiating an array of objects does not imply instantiating each of its elements. So it gives NullPointerException when you try to access aluno[numero_de_alunos] .

In the case of nota[i] this does not happen because it is an array of int , which is a primitive type, BUT you have to instantiate the array anyway, or else you have one exception to handle in followed.

You can do both in the loop for :

this.aluno = new Aluno[numero_alunos];

for(int i = 0; i < totalAlunos; i++)
{
    this.aluno[i] = new Aluno();
    aluno[i].nota = new int[numero_notas];
}
    
08.02.2014 / 15:19
3

To create the Array simply do the following: ArrayList<Aluno> listaDeAlunos; //ArrayList<DoTipoDaClasse> nomeDaLista;

Then you will instantiate:

listaDeAlunos = new ArrayList<Aluno>(numero_notas);
//numero_de_notas será o tamanho da lista.

Then to include notes in each position, substitute in the 2nd for:

//this.aluno[numero_de_alunos].nota[i] = Scan.nextInt();
listaDeAlunos.add(Scan.nextInt());

To get each note to the average:

//stack += this.aluno[NumeroAluno].nota[i];
stack += listaDeAlunos.get(i);

I hope I have helped.

    
08.02.2014 / 15:55