Member Avatar for mehnihma

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

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

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

Member Avatar for mehnihma

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

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?

Member Avatar for mehnihma

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

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

That seems to suggest that players is declared as a String - you haven't posted it declaration.

Member Avatar for mehnihma

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

... 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

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)

Member Avatar for mehnihma

Now when I have added

players.clear();

it works?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.