How to use arrays in Java?

Asked

Viewed 756 times

5

It’s a very beginner question (I’m new to Java, I came from C++). I have to do a program that takes 4 grades from 10 students, but when I go to work 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();
    }
}

Return:

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?

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].

Already 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 you will have one more Exception to deal with next.

You can do both in the loop itself 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];
}
  • 1

    Thanks :) I made a mistake, I did the opposite. But now, after putting all the numbers, I get: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4&#xA; at Nota.<init>(Test.java:28)&#xA; at Test.main(Test.java:59)&#xA;Java Result: 1. And in ideone, it gives others: https://ideone.com/mrilal

  • 1

    I updated the answer, check it out.

3

To create the Array just do so: 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, make the substitution below in 2° for:

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

To catch each note for the average:

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

I hope I’ve helped.

  • 1

    Oops, a little tricky for me, hahaha

  • 1

    Arraylist is a class and makes understanding easier, since it already has add, get, remove methods. If you use it you should import it: import java.util.Arraylist;

Browser other questions tagged

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