java notserializableexception但实现了serializable

时间:2017-10-06 09:50:22

标签: java sockets serialization serializable notserializableexception

我尝试使用套接字将对象写入服务器

我的对象"播放器"实现可序列化但如果我尝试编写该对象,则会出现一个不可序列化的异常。 objectoutputstream保存在对象" player"中,此输出流写入对象" player"但它会引发异常

package ch.tbz.robin.model;

import java.io.*;

import java.net.Socket;

public class Player implements Serializable{

    private Socket socket;
    private ObjectOutputStream out;
    private ObjectInputStream in;
    private String username;
    private String password;
    int id;
    int level;
    int health;
    int xp;
    int damage;

    public void setSocket(Socket socket) {
        this.socket = socket;
        try {

            out = new ObjectOutputStream(socket.getOutputStream());
            in = new ObjectInputStream(socket.getInputStream());

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public ObjectOutputStream getOut() {
        return out;
    }

    public ObjectInputStream getIn() {
        return in;
    }

    public Socket getSocket() {
        return socket;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public void setHealth(int health) {
        this.health = health;
    }

    public void setId(int id) {
        this.id = id;
    }

    public void setLevel(int level) {
        this.level = level;
    }

    public void setXp(int xp) {
        this.xp = xp;
    }

    public void setDamage(int damage) {
        this.damage = damage;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getUsername() {
        return username;
    }

    public String getPassword() {
        return password;
    }

    public int getLevel() {
        return level;
    }

    public int getHealth() {
        return health;
    }

    public int getXp() {
        return xp;
    }

    public int getDamage() {
        return damage;
    }
}

这里尝试编写"播放器"

package ch.tbz.robin.application;
import ch.tbz.robin.model.Attack;
import ch.tbz.robin.model.Player;
import ch.tbz.robin.model.Session;

import java.io.*;
import java.net.SocketException;

public class Game extends Thread implements Serializable{
  private Session session;
  public Game(Session session) {
      this.session = session;
      player1 = null;
      player2 = null;
  }
  private Player player1;
  private Player player2;

  public void run(){
      player1 = session.getPlayer1();
      player2 = session.getPlayer2();

      try {

        System.out.println("here");
        player1.getOut().writeObject(player1);
        player1.getOut().writeObject(player2);
        player2.getOut().writeObject(player2);
        player2.getOut().writeObject(player1);

      } catch (IOException e) {
          e.printStackTrace();
      }

      boolean notend = true;
      while(notend) {
          try {

              Attack attack1 = (Attack) player1.getIn().readObject();
              Attack attack2 = (Attack)  player2.getIn().readObject();

              player1.setHealth(player1.getHealth() - attack2.setDamage(player2.getDamage()));
              player2.setHealth(player2.getHealth() - attack1.setDamage(player1.getDamage()));

              if (player1.getHealth() < 0) {
                  notend = false;
              }
              if(player2.getHealth() < 0) {
                  notend = false;
              }

            player1.getOut().writeObject(attack1);
            player1.getOut().writeObject(attack2);
            player2.getOut().writeObject(attack2);
            player2.getOut().writeObject(attack1);

          } catch (SocketException e){
              System.out.println("user disconnected");
              notend = false;
          } catch (ClassNotFoundException e) {
              e.printStackTrace();
          } catch (IOException e) {
              e.printStackTrace();
          }
      }
  }
}

我该如何解决这个问题?

2 个答案:

答案 0 :(得分:0)

播放器中的所有字段都必须是可序列化的

答案 1 :(得分:0)

var app = new Vue({        
        el: '#app',
        data: {
            currentProduct: {
                categories: [
                  { categoryName: "",
                    subcatgoryName: ""
                  }
                ],

            },
            categories: [
                { categoryName: "Category 1" },
                { categoryName: "Category 2" }
            ],
            subcategories: [
                { "Category 1": [{ subcategoryName: "Subcategory 1" }, { subcategoryName: "Subcategory 2" }, { subcategoryName: "Subcategory 3" }] },
                { "Category 2": [{ subcategoryName: "Subcategory 4" }, { subcategoryName: "Subcategory 5" }, { subcategoryName: "Subcategory 6" }] }
            ]
        },
      methods: {
        addCategory: function () {
          this.currentProduct.categories.push({
            categoryName: "",
            subcategoryName: ""
          });
        }
      }
    });

由于异常消息已经告诉您,这些字段不是可序列化的。

您需要制作private ObjectOutputStream out; private ObjectInputStream in; ,或者更有可能将它们从transient移到其他地方。