mercredi 1 juin 2016

Casting Number to double primitive [duplicate]

This question already has an answer here:

I'm trying to access a HashMap<String, Number> via reflection:

Serializable obj; //here goes the HashMap
String name;
...
return (double)obj.getClass().getDeclaredMethod("get", Object.class).invoke(obj, name);

but so far all I got is a casting error caused by the line above:

java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Double

Indeed, the map value that was accessed by the key name was Integer.So I've changed the line to:

return obj.getClass().getDeclaredMethod("get", Number.class).invoke(obj, name).doubleValue();

but that didn't work out either. I even got doubleValue() underlined as "undefined for the type Object" (but why Object if I have Number.class?).

I'm not sure what casting rules I'm breaking. Can someone, please, help me if my map entries have various number values (Integer, Float, Double) but I need the method to return a double value (primitive).





Aucun commentaire:

Enregistrer un commentaire