Return Null Print Java

Asked

Viewed 80 times

-2

I’m developing an exercise where I need to create a class Ponto and add to an Array of points without me using ArrayList, when testing the first option in the menu I received a return that the array was Null even if I had already added a dot object in it.

This is my first code in Java and not quite sure the structure yet.

The files are all in the same Packages.

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        int tam;
        Scanner input = new Scanner(System.in);
        System.out.println("Digite por favor o tamanho da colecao :");
        tam = input.nextInt();
        ListaPonto lista = new ListaPonto();
        lista.listap(tam);
        while(true) {
            int opc;
            System.out.println("\n\n\n\n");
            System.out.println("\n------------------------------");
            System.out.println("\nProjeto Colecao de Elementos");
            System.out.println("\n------------------------------");
            System.out.println("\n0 - Encerrar");
            System.out.println("\n1 - Adicionar um elemento no Final");
            System.out.println("\n2 - Adicionar um elemento em uma posição");
            System.out.println("\n3 - Retornar indice da primeira ocorrencia de um elemento");
            System.out.println("\n4 - Remover um elemento em uma posicao");
            System.out.println("\n5 - Calcular a distantia dos 2 pontos mais distantes");
            System.out.println("\n6 - Colecao de pontos contidos em um Circulo");
            System.out.println("\n\n Informe sua opcao :");
            opc = input.nextInt();
        
            if(opc == 0) {
                break;
            }
            if(opc == 1) {
                Ponto p = new Ponto();
                int x,y;
                System.out.println("\nDigite o x do Ponto :");
                x = input.nextInt();
                System.out.println("\nDigite o y do Ponto :");
                y = input.nextInt();
                p.pontos(x, y);
                lista.adiciona(p);
                lista.printa();         
            }
    
        }
    }   
}

Here the Point Class is created

public class Ponto{
    private int x,y;

    public int pontos(int x, int y) {
        this.x = x;
        this.y = y;
    return x;
    }

}

The List Class is Created Here.

public class ListaPonto{
    private Ponto pontos[];
    private int validos;
    private int topo;


    public void listap(int N) {
        topo = N;
        this.pontos = new Ponto[N];
        this.validos = 0;   
    } 


    public void printa() {
        for (int i =0;i<=0;i++) {
            System.out.println(this.pontos[i]);
        }
    }


    public void adiciona(Ponto elem) {
        this.pontos[topo-1] = elem; 
    }


}
  • I didn’t see all the mistakes, but one thing you could do is create constructors in the classes (for example, in the class Ponto, instead of having the method pontos, create a constructor that receives x and y - the same goes for the method listap). See more details here

2 answers

1

When you initialize an array of nonprimitive elements in Java, all positions are filled with null. So when you perform this method:

lista.listap(tam);

You are initiating your array pontos. If at this point you were to go through it and print the contents of each position of it, you would have something like this:

null
null
null
...

Option 1 of your program says:

System.out.println("\n1 - Adicionar um elemento no Final");

When you run the line lista.adiciona(p);, your program adds at the end of the array the created point. If you re-print this array now, you would have something like this:

null
null
null
seu ponto aqui

However, his method printa() has a mistake:

for (int i =0;i<=0;i++) {

See, the way it is, you’re saying it’s for the for close when i<=0. Well, if i is initialized with 0, this means that it will print only the contents of the first position of your array, which, as I explained before, has the contents null, after all, at this point you have only the last position filled with the point you created.

For you to print all array positions, your for must be:

for (int i =0;i<topo;i++) {

or, better yet:

for (int i =0;i<this.pontos.length;i++) {

This ensures that all array positions will be printed.

0

I would advise you to create a constructor for Points with the parameters x and y. As follows:

public class Ponto {
private int x,y;

public Ponto(int x, int y) {
    this.x = x;
    this.y = y;
}

And when creating the object, wait for the console values

int x,y;
System.out.println("\nDigite o x do Ponto :");
x = input.nextInt();
System.out.println("\nDigite o y do Ponto :");
y = input.nextInt();
Ponto p = new Ponto(x, y);
lista.adiciona(p);
lista.printa(); 

Browser other questions tagged

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