I used an array in the parameter of a method and I do not know how to execute

2
import javax.swing.JOptionPane;

public class Cliente {

    private String nome;
    private String telefone;
    private int codigo;
    private String rua;

    void setRua(String z) {
        rua = z;
    }

    void setTelefone(String t) {
        telefone = t;
    }

    void setCodigo(int n) {
        codigo = n;
    }

    void setNome(String name) {
        this.nome = name;
    }

    int getCodigo(){

        return codigo;
    }

    void cadastroCliente(Cliente cliente, int n) {

        cliente.setCodigo(n);
        cliente.setNome(JOptionPane.showInputDialog("Nome: "));
        cliente.setTelefone(JOptionPane.showInputDialog("Telefone: "));
        cliente.setRua(JOptionPane.showInputDialog("Rua: "));
    }

    void removerCliente(Cliente cliente, int n) {

        cliente.setNome("");
        cliente.setTelefone("");
        cliente.setRua("");
    }

    void mostrarClientes(Cliente[] cliente) {



            for (int i = 0; i < cliente.length ; i++) {
                if ((cliente[i].getCodigo() != 0 ) && (cliente[i].nome != null)) {
                    JOptionPane.showMessageDialog(null, cliente[i].nome);
                }
            }
        }

    }

Below the main code .. where is my question ..

import javax.swing.JOptionPane;<
public class Operacoes {
public static void main(String[] args) {
Cliente teste = new Cliente();

 Cliente cliente[] = new Cliente[30];

cliente[1] = new Cliente();
cliente[2] = new Cliente();
cliente[3] = new Cliente();
cliente[1].cadastroCliente(cliente[1], 1); 
cliente[2].cadastroCliente(cliente[2], 2); 
cliente[3].cadastroCliente(cliente[3], 3); 

teste.mostrarClientes(); // Não sei o que colocar dentro do () 

} 
}
    
asked by anonymous 01.02.2015 / 20:45

2 answers

3

I think that's all you want.

teste.mostrarClientes(cliente);

You can change this method to filter what does not have initialized values. Not the right one but it works.

if (cliente[i] != null && (cliente[i].getCodigo() != 0 ) && (cliente[i].nome != null)) {

With this check you will skip all null elements in the array , regardless of their position. Ideally, you should not have null elements. I should just let it be for some very good reason. But to do what you want you'd need a ArrayList<E> on of the array . Arrays are very primitive and limited. They have their use but should not be used when you need flexibility.

If you are still going to use array , since it is starting. It helps to get the array started at index 0 instead of 1 as it was originally done. But it's best to make sure you do not make a mistake if you find a situation like this because they are possible.

    
01.02.2015 / 21:24
2

A NullPointerException is generated simply because the 0 position of your array is null. The problem starts here:

cliente[1] = new Cliente(); // Mas e a posição '0' ?!

When the mostrarClientes method is executed, it will begin to traverse the array from the zero position to the size of that array:

void mostrarClientes(Cliente[] cliente) {
   for (int i = 0; i < cliente.length ; i++) {
      // 0? NullPointerException...
   }
}

To solve the problem, simply instantiate the first object and insert it in the zero position of the array.

public class Operacoes {

    public static void main(String[] args) {
        Cliente teste = new Cliente();

        Cliente cliente[] = new Cliente[30];

        cliente[0] = new Cliente(); //...
        cliente[1] = new Cliente();
        cliente[2] = new Cliente();
        cliente[0].cadastroCliente(cliente[0], 1); //...
        cliente[1].cadastroCliente(cliente[1], 2);
        cliente[2].cadastroCliente(cliente[2], 3);

        teste.mostrarClientes(cliente); // E aqui, você passa o array de 'Cliente'
    }
}

OBS: Give self-explanatory names to your variables / attributes / classes / methods / whatever, this will help both you and anyone else.     

02.02.2015 / 07:09