简体   繁体   中英

java notserializableexception but implemented serializable

I try to write an object to my server with sockets,

my object "Player" implemtents Serializable but if I'm try to write the object there is an notserializableexception. the objectoutputstream is saved in the object "player", this outputstream write the object "player" but it throws an exception

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;
    }
}

here it trys to write the "Player"

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();
          }
      }
  }
}

how can I fix this?

Player中的所有字段都必须是可序列化的

private ObjectOutputStream out;
private ObjectInputStream in;

As the exception message will already have told you, these fields are not Serializable.

You need to make them transient , or more probably remove them from Player to somewhere else.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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