0
Vector<Vector<Integer>> wavesInformation;

for(Vector<Integer> waveInformation : wavesInformation) {

    for(Integer enemyIndex : waveInformation) { 

    }

}

Gives run time error:

Exception in thread "LWJGL Application" java.lang.ClassCastException: java.lang.Float cannot be cast to java.lang.Integer

In for(Integer enemyIndex : waveInformation) { line.

Thats rather confusing, because no float is used.

Writing

for(Float enemyIndex : waveInformation) {   

Gives compilation error:

Type mismatch: cannot convert from element type Integer to Float

EDIT:

I look up wavesInformation in debug mode, and discovered that stored numbers are really floats (0.0).

EDIT2:

Well, that is really strange.

For experimentation purposes I changed for loop to:

for(int i = 0 ; i < waveInformation.size() ; i++) {

I tried to assing a value from waveInformation to a variable like this:

float x = waveInformation.get(i);

and:

int x = waveInformation.get(i);

I've got the same error.

SOLUTION:

The problem was with Json parser (libgdx). I have read that libgdx json has limited support with nested generics and it only supports the first level. Eg, with ArrayList<Integer> it works, and values are read as Integers (but json.setElementType(Mission.class, "wavesInformation", Integer.class); line must appear (LIBGDX JSON specific). ArrayList<ArrayList<Integer>> will not work for now.

The solution is to add java.lang.Integer, before any number of that type in json file. Also for substitute - Gson (Google Json parser) supports nested generics. Other solution is more complicated and needs writing custom serialization method (read more).

Because I want to use libgdx json parser for now I have changed to `List>' and I'm casting each value to Integer (temporary solution).

Zaimatsu
  • 66
  • 15

1 Answers1

9

You1 have most likely ignored or suppressed some compiler warnings about unsafe casting, and your Vector<Integer> really contains some Float objects. Code that uses generics is not guaranteed to be statically type-safe if you ignore those warnings.


1 - In this case, it was a library that you are using that did this ...

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • can you explain "your Vector really contains some Float objects"? for my learning goal I asked it thank you. – Kick Buttowski Jul 22 '14 at 14:58
  • Well, everything that I do to my `Vector> wavesInformation;` is importing it from file with Json formatting. I think that has nothing to do with error because, it is written clearly, that I am using Integer. Also, no warrnings are related to my problem. – Zaimatsu Jul 22 '14 at 15:02
  • Which framework/tool are you using to deserialize/unmarshal the JSON (i.e. convert it from JSON to a Java representation)? At runtime, all generic type information (, etc.) will have been discarded due to type erasure, so the tool you are using to unmarshal the JSON may be incorrectly guessing the type to be Float, rather than Integer. – sumitsu Jul 22 '14 at 15:35
  • I'm using Json classes from libgdx library. Yeah, You are right - that parser is the problem. Thank You for Your help. Now I need to find how to specify that I'm using Integer. As soon as I find the solution, I will post it here. – Zaimatsu Jul 22 '14 at 15:44