java notserializableexception 但实现了 serializable
java notserializableexception but implemented serializable
我尝试使用套接字将一个对象写入我的服务器,
我的对象 "Player" 实现了可序列化,但如果我尝试编写该对象,则会出现不可序列化异常。 objectoutputstream保存在对象"player"中,这个outputstream写入对象"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;
}
}
此处它尝试写入 "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();
}
}
}
}
我该如何解决这个问题?
Player 中的所有字段都必须是可序列化的
private ObjectOutputStream out;
private ObjectInputStream in;
正如异常消息已经告诉您的那样,这些字段 不可序列化。
您需要制作它们 transient
,或者更可能将它们从 Player
移到其他地方。
我尝试使用套接字将一个对象写入我的服务器,
我的对象 "Player" 实现了可序列化,但如果我尝试编写该对象,则会出现不可序列化异常。 objectoutputstream保存在对象"player"中,这个outputstream写入对象"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;
}
}
此处它尝试写入 "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();
}
}
}
}
我该如何解决这个问题?
Player 中的所有字段都必须是可序列化的
private ObjectOutputStream out;
private ObjectInputStream in;
正如异常消息已经告诉您的那样,这些字段 不可序列化。
您需要制作它们 transient
,或者更可能将它们从 Player
移到其他地方。