mardi 28 février 2017

JAVA - Set NULL using reflection

I need to store a NULL value when a null value is retrieved from the database. I'm using the reflection to generalize the type of data than can be stored. For example:

if(rs.getObject("A") != null)
    methodA.invoke(object, methodResultSet.invoke(rs, "A"));
else
    methodA.invoke(object, indexes,"WEATHER_OBS_REGION",null);

methodA is used to manage the main data type, so at runtime it can have the following values:

methodAbstractClass = myClass.getDeclaredMethod("setValueInt,int.class)
methodAbstractClass = myClass.getDeclaredMethod("setValueDouble,double.class)
methodAbstractClass = myClass.getDeclaredMethod("setValueString,String.class)

In the null case (see else above), I also tried

methodA.invoke(object, indexes,"WEATHER_OBS_REGION", new Object[]{null});
methodA.invoke(object, indexes,"WEATHER_OBS_REGION",Double.Nan);

obtaining several errors:

java.lang.IllegalArgumentException

or

java.lang.IllegalArgumentException: argument type mismatch

Is there a general way to set NULL, regardless of the type of data to be stored?

Thanks





Aucun commentaire:

Enregistrer un commentaire