I used an array in a method parameter and do not know how to run

Asked

Viewed 145 times

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 code main.. where is my doubt..

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 () 

} 
}
  • 1

    I already told you that the structure of your code is all wrong. You are mixing things. It seems strange to me that you can create array, create a method that receives a array and does not know how to pass the array as argument. I think you need to take a programming courses before you start trying to program. Your name is Igor or Rodrigo? http://answall.com/users/21284/igor-gutemberg

  • Related: http://answall.com/questions/10869/como-chamar-um-m%C3%A9all-when-your-even%C3%A2metro-%C3%A9-a-array

  • Take a look at [tour]. You can accept an answer if it solved your problem. You can vote on every post on the site as well. Did any help you more? You need something to be improved?

2 answers

3

I think that’s all you want.

teste.mostrarClientes(cliente);

You can change this method to filter what has no initialized values. It is not the most correct but it works.

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

I put in the Github for future reference.

With this check you will skip all the null elements in the array, no matter your position. The ideal is that it has no null elements. You should only let it have for some very good reason. But to do what you wish would take a ArrayList<E> in the breath of array. Arrays are very primitive and limited. They have their use but should not be used when you need flexibility.

If you’re still using the array, already it’s starting. It helps to get popular the array by index 0 instead of 1 as originally done. But it’s best to make sure you don’t make a mistake if you run into a situation like this, because they’re possible.

  • When you say, "I’ve already told you that the structure of your code is all wrong." be more specific Igor is my group friend from technical school we have not had array class we are still trying to create this project.. Could you give us some tips? What you said in reply returned this error.. " Exception in thread "main" java.lang.Nullpointerexception at Client.show At operations.main(Operations.java:19) Java Result: 1"

  • Here is another problem. Here is not a forum, here we have only objective questions, focused so that the answers are like this too. When passing error information, indicate which line is because we have no way of knowing the exact line number. This error occurs because you are passing one array with 30 elements but is only initiating 3 of them. The error occurs when going to access the room that is not initialized, there it finds a null element and breaks. Either you should initialize all elements or switch to a list that allows you to add new elements as needed

  • Comments should not be used to post codes, so don’t post. You’re already asking other questions. See [tour] and [help]. I broke your branch in one part.

2

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

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

When the method mostrarClientes run, it will start to scroll through the position array zero up to the size of this 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 into 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 for your variables/attributes/classes/methods/whatever, this will help both you and those who help you.

Browser other questions tagged

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