NullPointerException when adding a "Vehicle" in Java

1

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.

    
asked by Lucas. D 28.07.2016 в 23:16
source

4 answers

1

I do not think the problem is that the variable vehi is at null , as you have been told you can add an object null to a ArrayList .

I think the problem of NullPointerException is more in some element of the form and when you do the data collection. What line of code is this? java.lang.NullPointerException at interfaz.FormCargar.btnCargarActionPerformed(FormCargar.java‌​:144)

    
answered by 04.01.2017 в 10:02
0

gives you that error because the vehicle you are initializing with null and at some point when you evaluate in the switch the typeVehicle it is possible to go to the default option that would indicate that you would be adding to your collection of the parent class a null.

    
answered by 30.10.2016 в 17:08
-1

I recommend that you always verify your objects before doing any operation with them, I'll give you an example:

ArrayList<Vehiculo> vehiculos = new ArrayList<>();

public void agregarVehiculo(Vehiculo nuevo) {
  if(nuevo != null){ //verifica si el objeto tiene valor
     vehiculos.add(nuevo);
  }
}

These are good practices that you have to get used to especially in the java language.

    
answered by 29.07.2016 в 14:37
-1

This line is the one that possibly gives you problems.

int tipoVehiculo     =     cboTipo.getSelectedIndex() + 1;

If you have not added selected value for your combo or List cboTipo.getSelectedIndex() will return -1 to which you will then add one to convert it to zero and as that option does not exist in your switch your variable vehi will be null.

There are two approaches to use here, or you use the "sentinel" design pattern that would consist of adding an option with a flag value (a "select" option that is selected by default).

Or use the design pattern commonly known as "client-side validation", which consists of not sending the information to be processed until all the fields have valid values.

If this does not solve your problem, you may not have started your concessional variable.

    
answered by 27.10.2017 в 00:15