即使我发送的所有对象都是Serializable,也会获得NotSerializableException

时间:2013-05-21 04:30:26

标签: java serializable

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

这是我的代码:

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

如果您需要我的其他课程,请告诉我。

谢谢!

3 个答案:

答案 0 :(得分:4)

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

答案 1 :(得分:3)

您的类EventWindowGameActual的内部类,因此每当您尝试序列化Event实例时,WindowGameActual及其所有字段的封闭实例也将被序列化。

解决方案:除非确实合适,否则不要使用内部类,将Event类提升为自己的文件。

答案 2 :(得分:-1)

看一下XStream http://x-stream.github.io/它具有与序列化/反序列化相同的功能,除了你正在处理XML。不需要类来实现Serializable。这将解决您的所有问题,无需任何解决方法。

相关问题