java notserializableexception但实现了可序列化
Posted
技术标签:
【中文标题】java notserializableexception但实现了可序列化【英文标题】:java notserializableexception but implemented serializable 【发布时间】:2018-03-18 02:21:48 【问题描述】:我尝试使用套接字将对象写入我的服务器,
我的对象“Player”实现了可序列化,但如果我尝试编写该对象,则会出现 notserializable 异常。对象输出流保存在对象“播放器”中,此输出流写入对象“播放器”但它抛出异常
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();
我该如何解决这个问题?
【问题讨论】:
您应该将遇到的异常的堆栈跟踪添加到问题中。 【参考方案1】:private ObjectOutputStream out;
private ObjectInputStream in;
正如异常消息已经告诉您的那样,这些字段不可序列化。
您需要将它们设为transient
,或者更可能将它们从Player
删除到其他地方。
【讨论】:
我不知道我必须添加瞬态【参考方案2】:Player 中的所有字段都必须是可序列化的
【讨论】:
以上是关于java notserializableexception但实现了可序列化的主要内容,如果未能解决你的问题,请参考以下文章