tags:

views:

64

answers:

3

Hi,

I have an array list of objects in my application.

private static ArrayList<Player> userList=new ArrayList<Player>();

In my application, I am converting this list to byte array and then sending it to other clients. At client When I am trying to cast it back to the ArrayList, its giving me casting error. I am doing this in client side after receiving this list as byte array:

ArrayList<Player> pl = (ArrayList<Player>) toObject(receivedByteArray); 

where toObject is my function to convert the byte array to object;

Any Suggestions please !!!

Thanks.

java.lang.ClassCastException: Player cannot be cast to java.util.ArrayList
        at Client.attemptLogin(Client.java:242)
        at Client.main(Client.java:64)
+2  A: 

You should use Arrays.asList () instead:

List<Player> pl = Arrays.asList (toObject(receivedByteArray));

UPD: I'm not sure if it's only a naming issue: can you show the code, where you send/receive players? Does receivedByteArray contain Players or it really contains bytes? If second then there is no straightforward solution. Can you show a stacktrace?

Roman
Doesn't work. Why this is upvoted 3 times is beyond me.
BalusC
@BalusC: see my UPD (written before I've seen your comment)
Roman
`receivedByteArray` is clearly a `byte[]`. An `Arrays#asList()` of it would only give a `List<byte[]>` with 1 item. Your answer is far from logical.
BalusC
@BalusC: hehe. I don't know what he does inside toObject and how the objects are being send but after posting the stacktrace I understand that *my code do work*.
Roman
Pure coincidence. Other chance is that the one side converted the list of players in an incorrect manner so that it only emits the first player instead of the list or so. Btw: Tara is a maiden name, so be gentle to say *she* ;)
BalusC
Balu Sir read the last name also , :D Tara SINGH here ...
Tara Singh
@BalusC: ups)) I've even tried to edit the comment but there is some time limit exists probably that I can't do that.
Roman
Thanks Roman for your help.
Tara Singh
+1  A: 

I bet that you're talking about serialization, since that's the standard way to transfer Java objects as byte streams over some interfaces. I also assume that your Player class already implements java.io.Serializable, else you would have faced a NotSerializableException.

If you get a ClassCastException on one of the sides, then it means that the class file representing the Player class is not of exactly the same version. To fix this, you need either to ensure that the both sides are using exactly the same class file, or to add a private static final long SerialVersionUID with the same value to the class on the both sides.

Update as per the actual exception:

java.lang.ClassCastException: Player cannot be cast to java.util.ArrayList

It means that you're basically trying to do the following:

ArrayList<?> list = (ArrayList<?>) object;

where object is actually a Player. To fix this, you need to ensure that object is actually an ArrayList<?>, or you need to cast to Player instead.

BalusC
@BalusC: are you sure that ClassCastException is thrown when there are different versions of a class on client and server side? I don't remember exactly, but google says it's InvalidClassException http://java.sun.com/javase/6/docs/api/java/io/InvalidClassException.html. Anyway it's difficult to guess the real problem without seeing full picture (i.e. send/receive code and stacktrace)
Roman
@Roman: yes, sure. The `InvalidClassException` is only thrown if you **already** have definied a `SerialVersionUID`. It is also only thrown during **deserializing**, not during casting.
BalusC
The class is Serialized and also the same class is used at Server and Client. Object here is for Sure ArrayList<Player> as This is the thing I am converting to byte array at server side, and thus On Client side, to catch these bytes I need to cast them into ArrayList<Player>. But in doing this its throwing the casting error :(
Tara Singh
If you get a `Player` instead of `ArrayList<Player>` in the client, then it means that the server has sent a `Player` instead of `ArrayList<Player>` to the client. Verify server's code.
BalusC
Thank You BalusC, your analysis helped a lot. There was an error in sending the Player arraylist. It was sending player only. Now its Working.. Thanks a lot :)
Tara Singh
A: 

byte[] can not be casted to/from ArrayList. If you need to represent your collection as a byte sequence, you probably need to serialize it. Use ObjectOutputStream and ObjectInputStream.

Eyal Schneider