Error doing a subtraction on DAO JAVA jpa

0

I have a DAO (Data Access Object) to create a history. What I'm looking for is to create a history, subtract it and put that value in a variable.

However, I get nullpointerexception. Is there a problem with my code?

   public boolean crearHistorial2(Equipo equipo, Usuario usuario, Integer kilo, String ubi, Integer kilore) {
    Historial historial = new Historial();
    historial.setEquipo(equipo);
    historial.setUbicacion(ubi);
    historial.setUsuario(usuario);
    historial.setKilometrost(kilo);
    int valor1;
    valor1= Integer.parseInt(equipo.getKilometrost());
    historial.setKilometros(kilo - valor1);
    historial.setFecha(new Date());
    return jpam.persistirEntidad(historial);
}

The error is thrown into the subtraction and I want the value of that subtraction to have the kilometers attribute.

    
asked by Alexander Villalobos 23.02.2017 в 14:50
source

1 answer

0

if the error gives you here history.setKilometers (kilo - value1); And it gives you a nullPointer, I understand that one of the two values is null, use the debugger and see what value the two fields have before executing the subtraction.

Now, I see a strange behavior in your code, if kilo is an Integer and value1 an int, why do you subtract them ?, maybe it's easier if you create value1 as Integer directly and you avoid the strange cast that you had what to do to assign value to value1.

I would look at the value of the two fields to verify that none is null and try to make value1 Integer to do the subtraction Integer - Integer and not int - Integer.

Greetings.

    
answered by 23.02.2017 в 15:34