I've spent hours on this bug, this is a very strange behavior for me :
Here is my code :
Map<String, Float> myMap = myService.calculateData(DateTime startDate, DateTime endDate);
if (!myMap.isEmpty()) {
Double value1 = myMap.get("key1").doubleValue();
}
In the debugger, everything works fine, I can see all the values of myMap
and I can get the value of value1
as a Double.
But then, IntelliJ throws the error :
java.lang.ClassCastException: java.lang.Double cannot be cast to java.lang.Float
I tried replacing Double value1 = myMap.get("key1").doubleValue();
with : Float value1 = myMap.get("key1");
and I get the exact same error, even though I'm not using any Double !
Also I don't understand why it says Double cannot be cast to Float, when I'm actually trying to convert a Float into a Double, so it's the other way around.
UPDATE : to answer to Louis and Andreas suggestions, myMap.get("key1").getClass()
gives me Double
, myMap.get("key1") instanceof Double
equals true
and myMap.get("key1") instanceof Float
equals false
.
I get the error on the line Double value1 = myMap.get("key1").doubleValue();
or on Float value1 = myMap.get("key1");