2

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");

Aleksandr M
24.4k13 gold badges75 silver badges148 bronze badges
asked Dec 2, 2015 at 21:13
4
  • 1
    Where does the exception occur? Maybe in a line you didn't post? Commented Dec 2, 2015 at 21:16
  • 1
    Please post the entire stack trace; Float value1 = myMap.get("key1"); AFAIK cannot throw that exception. Commented Dec 2, 2015 at 21:18
  • That can't be your actual code, as it is not syntactically valid. Please can you post a Minimal, Complete, Verifiable Example by which we can reproduce your issue. Commented Dec 2, 2015 at 21:19
  • @DaveNewton, yes I also don't understand how Float value1 = myMap.get("key1"); can throw that exception... this is driving me crazy ! I'll check what happens in myService.calculateData and if that doesn't work, I'll post the entire stack trace. Commented Dec 2, 2015 at 21:49

1 Answer 1

5

This would be a normal symptom for some corruption in how the Map from myService.calculateData was constructed in the first place; it's not really a Map<String, Float>, it's got Doubles in it. That's usually an indication that there were some suppressed unsafe casts or rawtypes.

The cast to Float would be implicitly inserted into the map.get("key1") call at compile time, as part of how generics are normally implemented.

answered Dec 2, 2015 at 21:19
Sign up to request clarification or add additional context in comments.

3 Comments

Confirm by doing System.out.println(myMap.get("key1").getClass()).
Yes Louis and Andreas, I think you're right because myMap.get("key1").getClass() gives me Double , myMap.get("key1") instanceof Double equals true and myMap.get("key1") instanceof Float equals false. So can the problem be fixed in myService.calculateData only ? Not in my code ?
@LouisWasserman , thanks a lot, the problem was indeed in myService.calculateData . You saved me some hours of debugging ! :)

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.