简体   繁体   English

Java对象-> String []到String []

[英]Java Object->String[] to String[]

I have an Object variable that contains a String[] array, and I am trying to do the following 我有一个包含String []数组的Object变量,并且我正在尝试执行以下操作

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

I however recieve the dreaded ClassCastException. 但是,我收到了可怕的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[]. 我正在尝试将对象转换为String []。

If you get a ClassCastException, it means that the packet does NOT hold a String[] . 如果收到ClassCastException,则表示该数据包不包含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. 调试器将为您提供此信息,而无需修改代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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