How to verify that vector fields are null?

Asked

Viewed 210 times

-1

I am building a program, which should initially check whether the array of type (Class) is null.
My class has the get and set method, as well as the constructors with and without standard.
However, when the program initializes, the values of each cell in my array should be null for the orderly filling of the data.

But it is precisely at this point where the error occurs.

If I put that mine vetor[i] == null, the program jumps to the end and does not execute the command.

For the command run I’m putting vetor[i] != null, but this way I’m overwriting some data.

How do I resolve?

public void Insere(){

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

          estacionamento[i] = new Veiculo();

        if(estacionamento[i] != null){

         estacionamento[i].setPlaca(JOptionPane.showInputDialog("informe a placa do veiculo"));
         estacionamento[i].setModelo(JOptionPane.showInputDialog("informe o modelo do veiculo"));



        }else{
        JOptionPane.showMessageDialog(null, " não ha vagas no estacionamento");
        } break;
    }
}
  • 2

    Why do you have a break there at the end?

  • 1

    It’s a little complicated to understand what you want. Even because you have this if, may even have a reason. There are cases that the creation of Veiculo fail? Is there a reason for this? It seems to me that there are conceptual errors before running errors in this code.

  • The code altogether compiles without any problem! However in claisula if it runs the way it jumps to the part after Else. Which is not correct once the parking array is empty. The only way I found (incorrect) is to put the parking[i] != null...... This way it reads and executes as I want. But it allows me to put as many objects as you want overwriting the existing ones.

  • break; from the end is for the command drop and return from the main menu in the main class

1 answer

0

Your intentions with the code are confused.

My class has the get and set method, as well as the builders with and without standard. However, when the program initializes, the values of each cell in my array should be null for the orderly filling of the data.

Are you planning to use the setPlaca and setModelo but you want to initialize the array with null values. This is not possible, you need to have the instance of a Vehicle at each position of the array.

Still, I believe you are planning to have the following code. First, to simulate the array with null values:

// Simulando a inicialização de um array com valores nulos.
Veiculo[] estacionamento = new Veiculo[3];
estacionamento[0] = null;
estacionamento[1] = null;
estacionamento[2] = null;

And the code in question:

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

  if(estacionamento[i] == null){
    estacionamento[i] = new Veiculo(); //faltou instanciar Veiculo
    estacionamento[i].setPlaca(JOptionPane.showInputDialog("informe a placa do veiculo"));
    estacionamento[i].setModelo(JOptionPane.showInputDialog("informe o modelo do veiculo"));

  } else {
    JOptionPane.showMessageDialog(null, " não ha vagas no estacionamento");
  } 
  break;
}
  • Setting null values for each position is unnecessary, since a non-priminal type array is initialized with its already null positions.

  • @Statelessdev, it was just to simulate his case. I don’t know if it had values on this vector before, reusing an existing vector (there is no such part of the code available), so I set null to just for demonstration.

  • It is exactly this reasoning.... It really lacked instantiating the "Vehicle". However, now he returns me the message of Else for all filled vacancies, how do I solve this question so that the message appears only once? An accountant with another if would solve?

Browser other questions tagged

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