lundi 9 janvier 2023

"IllegalArgumentException: Can not set java.lang.Long field to null value" when given value is a primitive

I'm trying to write a Hibernate ResultTransformer to fill a class from an Object[] using field.set(). When the field is Long but the value is long it gives me the error in the title. I've confirmed the value isn't actually null, it's just a primitive. Theoretically Java should be autoboxing that, but that doesn't seem to be the case? Is there any way to do this without manually checking the value's type and wrapping it with the appropriate wrapper when needed (i.e. a big if-else chain)?

Map<String, Object> values = ...; // key will be the column name

for(Field field : cls.getFields()) {
    Column c = field.getAnnotation(Column.class);
    if(c == null) continue; // I get past this so this definitely isn't the problem.

    field.set(objectToFill, values.get(c.name()); // If field is Long but value is long, I get error in title.
    // values.get(c.name()) is working fine and returning a non-null long value.
}




Aucun commentaire:

Enregistrer un commentaire