I'm practicing a bit of Java, so I set out to do a little program to add Vehicles and inheritance. But clicking on the "Load" button throws me a NullPointerExcpetion
.
My code for the Load button:
String nombreTitular = txtNombreTitular.getText();
int dni = Integer .parseInt(txtDni.getText());
String patente = txtPatente .getText();
int modelo = Integer .parseInt(txtModelo.getText());
int tipoVehiculo = cboTipo .getSelectedIndex() + 1;
Vehiculo vehi = null;
switch (tipoVehiculo) {
case 1:
vehi = new Auto (nombreTitular, dni, patente, modelo, tipoVehiculo);
break;
case 2:
vehi = new Moto (nombreTitular, dni, patente, modelo, tipoVehiculo);
break;
case 3:
vehi = new Camioneta(nombreTitular, dni, patente, modelo, tipoVehiculo);
break;
default:
break;
}
concesionaria.agregarVehiculo(vehi);
JOptionPane.showMessageDialog(this, "Se ha cargado un nuevo vehiculo");
Each Vehiculo
, whether Moto
, Auto
or Camioneta
have their respective set , get , and a constructor with parameters, for example this is the Moto
.
public Moto(String nombreTitular, int dni, String patente, int modelo, int tipoVehiculo) {
super(nombreTitular, dni, patente, modelo, tipoVehiculo);
this.tipoVehiculo = tipoVehiculo;
}
And in my class Concesionaria
I have methods, a ArrayList
and my method to add a new Vehiculo
ArrayList<Vehiculo> vehiculos = new ArrayList<>();
public void agregarVehiculo(Vehiculo nuevo) {
vehiculos.add(nuevo);
}
I have tried everything, but the error persists. For some reason I'm taking some field as empty. If there is any way to share the whole project for better help, I would appreciate the means to do it without complications.