gpt4 book ai didi

java - 即使我发送的所有对象都是可序列化的,也收到 NotSerializedException

转载 作者:行者123 更新时间:2023-12-01 08:11:47 25 4
gpt4 key购买 nike

好吧,我有一个扩展 JFrame 的类,我基本上尝试创建这个 JPanel,它接收信息并在按下“提交”按钮时发出“事件”对象。一切似乎都正常,除了当我按下提交时,它告诉我我的客户端需要可序列化...(Client 类是一个基本类,它打开到特定端口的连接。它有效,我已经测试过它这不是问题)。我已经序列化了我要发送的所有对象。我不明白为什么我会收到 NotSerializedException。几个小时以来一直试图解决这个问题。任何见解将不胜感激。

这是我的代码:

public class WindowGameActual extends JFrame implements ActionListener
{
private final static int PORT = 11114;

private GameState game;

public WindowClient connection;

JPanel container;


JTextArea description;
JTextField difficulty;
JCheckBox check4;
JCheckBox check6;
JCheckBox check8;
JCheckBox check12;
JCheckBox check20;

JCheckBox agl;
JCheckBox str;
JCheckBox mana;


public class WindowClient extends Client
{
public WindowClient(String host) throws IOException
{
super(host, PORT);
}


protected void messageReceived(Object message)
{
if(message instanceof GameState)
{
game = (GameState) message;
container.repaint();
}

}
} //end WindowClient


public WindowGameActual(String host)
{

setDefaultCloseOperation(EXIT_ON_CLOSE);

game = new GameState();

try {
connection = new WindowClient(host);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

container = new JPanel();
GridLayout gridC = new GridLayout(3, 1);
container.setLayout(gridC);

CurrentEventPanel curEvt = new CurrentEventPanel(game);
curEvt.updateEvent(game);
container.add(curEvt);

// ControlPanel eventControl = new ControlPanel(host);
// container.add(eventControl);

GridLayout outerPanel = new GridLayout(4, 1);
JPanel control = new JPanel();
control.setLayout(outerPanel);

JPanel layer1 = new JPanel();

JLabel eDescription = new JLabel("Describe Event:");
layer1.add(eDescription);

description = new JTextArea("");
description.setEditable(true);
description.setColumns(17);
layer1.add(description);

control.add(layer1);

JPanel layer2 = new JPanel();

JLabel eDifficulty = new JLabel("Event Difficulty (integers only):");
layer2.add(eDifficulty);

difficulty = new JTextField("");
difficulty.setEditable(true);
difficulty.setColumns(2);
layer2.add(difficulty);

JButton submit = new JButton("Submit");
submit.addActionListener(this);
submit.setPreferredSize(new Dimension(74, 22));
layer2.add(submit);

control.add(layer2);


JPanel layer3 = new JPanel();

JLabel dice = new JLabel("dice: ");
layer3.add(dice);

JLabel d4 = new JLabel("d4");
check4 = new JCheckBox();
layer3.add(d4);
layer3.add(check4);

JLabel d6 = new JLabel("d6");
check6 = new JCheckBox();
layer3.add(d6);
layer3.add(check6);

JLabel d8 = new JLabel("d8");
check8 = new JCheckBox();
layer3.add(d8);
layer3.add(check8);

JLabel d12 = new JLabel("d12");
check12 = new JCheckBox();
layer3.add(d12);
layer3.add(check12);

JLabel d20 = new JLabel("d20");
check20 = new JCheckBox();
layer3.add(d20);
layer3.add(check20);

control.add(layer3);

JPanel layer4 = new JPanel();

JLabel skills = new JLabel("Skills Required: ");
layer4.add(skills);

JLabel strLabel = new JLabel("Str");
str = new JCheckBox();
layer4.add(strLabel);
layer4.add(str);

JLabel aglLabel = new JLabel("Agl");
agl = new JCheckBox();
layer4.add(aglLabel);
layer4.add(agl);

JLabel manaLabel = new JLabel("Mana");
mana = new JCheckBox();
layer4.add(manaLabel);
layer4.add(mana);

control.add(layer4);

container.add(control);
// setPreferredSize(new Dimension(200, 20));

UserStatPanel stats = new UserStatPanel(game);
stats.updateStatPanel(game);
container.add(stats);


add(container);
setVisible(true);
}


public class Event implements Serializable
{
public String eventDescription ;
public String diff ;

public boolean strChecked ;
public boolean aglChecked ;
public boolean manaChecked ;

public boolean d4checked ;
public boolean d6checked ;
public boolean d8checked ;
public boolean d12checked ;
public boolean d20checked;

public Event()
{
eventDescription = description.getText();
diff = difficulty.getText();
strChecked = str.isSelected();
aglChecked = agl.isSelected();
manaChecked = mana.isSelected();
d4checked = check4.isSelected();
d6checked = check6.isSelected();
d8checked = check8.isSelected();
d12checked = check12.isSelected();
d20checked = check20.isSelected();
}


}
Event nEvent;

public void actionPerformed(ActionEvent evt)
{
String cmd = evt.getActionCommand();
if(cmd.equals("Submit"))
{
nEvent = new Event();
connection.send(nEvent);
}


}



}

这是我的错误消息(异常短):

Client send thread terminated by IOException: java.io.NotSerializableException: testGame.WindowGameActual$WindowClient
Client send thread terminated.
Client receive thread terminated.
Hub receive thread terminated by IOException: java.io.WriteAbortedException: writing aborted; java.io.NotSerializableException: testGame.WindowGameActual$WindowClient

如果您需要我的任何其他类(class),请告诉我。

谢谢!

最佳答案

异常消息清楚地表明 WindowClient 不可序列化。最合理的修复方法是使 Client(WindowClient 扩展)可序列化

关于java - 即使我发送的所有对象都是可序列化的,也收到 NotSerializedException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16662183/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com