0

I have an Object variable that contains a String[] array, and I am trying to do the following

String[] arr = ((String[])packet.getObject());

I however recieve the dreaded ClassCastException. What information do you guys need? My packet class looks as the following:

public class Packet implements Serializable {
private static final long serialVersionUID = 1L;
private int type;
private Object object;

public Packet(int type, Object object) {
    this.type = type;
    this.object = object;
}

public int getType() {
    return type;
}

public void setType(int type) {
    this.type = type;
}

public Object getObject() {
    return object;
}

public void setObject(Object object) {
    this.object = object;
}
}

Any ideas? I'm trying to convert the Object into a String[].

3
  • 2
    What type is passed into the constructor of Paket at runtime (or set via setter)? I guess it is not a String[] ;) Commented Oct 27, 2013 at 7:48
  • Oh damn, thank you! Just realized it's inside another Class, DOH! Commented Oct 27, 2013 at 7:51
  • 1
    In which case, the ClassCastException will tell you which class you really have. Commented Oct 27, 2013 at 7:53

1 Answer 1

3

If you get a ClassCastException, it means that the packet does NOT hold a String[].

Execute the following code to see what it actually holds, then debug to see why and where the object is stored in the packet:

System.out.println(packet.getObject());
System.out.println(packet.getObject().getClass());

A debugger would give you this information without needing to modify the code.

Sign up to request clarification or add additional context in comments.

1 Comment

Indeed it was not a String[] but another class I had it wrapped in, thanks :)

Your Answer

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.