简体   繁体   中英

Java Object->String[] to String[]

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM