-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;
}
}
Why do you have a
break
there at the end?– Jéf Bueno
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 ofVeiculo
fail? Is there a reason for this? It seems to me that there are conceptual errors before running errors in this code.– Maniero
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.
– Wailler Prata
break; from the end is for the command drop and return from the main menu in the main class
– Wailler Prata