0

I am trying to cast vector to a string

players = (Vector<Player>) inStream.readObject();

And I get error: java.lang.ClassCastException: java.util.Vector cannot be cast to java.lang.String

Can someone tell me is there a way to do it?

Thanks

3
Contributors
10
Replies
13
Views
4 Years
Discussion Span
Last Post by mehnihma
0

If the object being read is a String, cast it to String.

Why do you think the object being read is a Vector?

0

Because it is a vector list of Players and I get that error?

0

Where is the cast to String? I would expect to see: (String)

Please post the full text of the error message and the full text of the source line. What is the definition of players?

0

Vector<Player> players = new Vector<Player>();

That is the list in which I add player objects like players.add(new Player("1", 0, 0));

When I add them like this:
player = new Player(userLogin.getText() , -1, -1);

It works normaly but when I send them to server and recive updated list and read it :
players = (Vector<Player>) inStream.readObject();

i get this error:

java.lang.ClassCastException: java.util.Vector cannot be cast to java.lang.String

I am sending Strings and Objects, strings work normally

static String readString() {
    // read the message
    String s = null;
    try {

        s = (String) inStream.readObject();
    } catch (Exception e) {
        System.out.println(e);
    }

    return s;
}

but players not?


static void readPlayers() 
{
    try {

        players = (Vector<Player>) inStream.readObject();
    } catch (Exception e) {
        System.out.println(e);
    }
}

Edited by mehnihma: new

0

Which line does the error occur on? Line 6 has a cast to String: (String)
Please post the full text of the error message.

Edited by NormR1

0

Players is declared as static Vector<Player> players = new Vector<Player>();

and full error message is:

java.lang.ClassCastException: java.util.Vector cannot be cast to java.lang.String

Error is on players = (Vector<Player>) inStream.readObject(); when I try to read in the object

0

... and you're certain that that declaration isn't being masked anywhere?
OK. Try printing the inStream.readObject() at that point to see exactly what kind of object you are getting

0

Please post the full text of the error messsage with the stack trace.

An example:

Exception in thread "main" java.lang.ClassCastException: java.util.Vector cannot be cast to java.lang.String
at TestCode7.main(TestCode7.java:334)

Edited by NormR1

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.