尝试将自定义对象写入outputStream时获取java.io.NotSerializableException

时间:2015-12-03 16:19:01

标签: java objectoutputstream

try
{
    CompressedMessage cMessage = new CompressedMessage(messageArea.getText(), false);
    cMessage.compressMessage();         
    EncryptedMessage eMessage = new EncryptedMessage(MessageType.USERMESSAGE, "", cMessage);
    outputStream.writeObject(eMessage);
}
catch (IOException e1)
{
    System.out.println(e1);
    System.exit(1);
}

我正在尝试向服务器(本地atm)发送消息,在尝试发送EncryptedMessage对象之前(在字符串中)发送数据时没有问题。

public class EncryptedMessage
{
    private MessageType type;
    private String receipent;
    private CompressedMessage message;

    public EncryptedMessage(MessageType T, String R, CompressedMessage mes)
    {
        type = T;
        receipent = R;
        message = mes;
    }

    public MessageType getType()
    {
        return type;
    }
    public void setType(MessageType type)
    {
        this.type = type;
    }
    public String getReceipent()
    {
        return receipent;
    }
    public void setReceipent(String receipent)
    {
        this.receipent = receipent;
    }
    public CompressedMessage getMessage()
    {
        return message;
    }
}

非常感谢任何帮助

1 个答案:

答案 0 :(得分:1)

EncryptedMessage和CompressedMessage应该实现Serializable