反序列化期间的 Kryonet 错误
Kryonet Error during deserialization
我正在尝试使用网络库 kryonet 创建一个多人游戏,我已经连接并发送字符串正常工作,但现在我正在尝试发送对象。我想要做的是发送一个数组列表,但它给了我这个错误。我也尝试只发送 1 个对象,但它给了我同样的错误。
Exception in thread "Client" com.esotericsoftware.kryonet.KryoNetException: Error during deserialization.
at com.esotericsoftware.kryonet.TcpConnection.readObject(TcpConnection.java:141)
at com.esotericsoftware.kryonet.Client.update(Client.java:247)
at com.esotericsoftware.kryonet.Client.run(Client.java:333)
at java.lang.Thread.run(Thread.java:745)
Caused by: com.esotericsoftware.kryo.KryoException: Class cannot be created (missing no-arg constructor): com.connorbrezinsky.spaceraiders.objects.Metor
at com.esotericsoftware.kryo.Kryo.newInstantiator(Kryo.java:1048)
at com.esotericsoftware.kryo.Kryo.newInstance(Kryo.java:1060)
at com.esotericsoftware.kryo.serializers.FieldSerializer.create(FieldSerializer.java:228)
at com.esotericsoftware.kryo.serializers.FieldSerializer.read(FieldSerializer.java:217)
at com.esotericsoftware.kryo.Kryo.readClassAndObject(Kryo.java:735)
at com.esotericsoftware.kryo.serializers.CollectionSerializer.read(CollectionSerializer.java:109)
at com.esotericsoftware.kryo.serializers.CollectionSerializer.read(CollectionSerializer.java:18)
at com.esotericsoftware.kryo.Kryo.readClassAndObject(Kryo.java:735)
at com.esotericsoftware.kryonet.KryoSerialization.read(KryoSerialization.java:58)
at com.esotericsoftware.kryonet.TcpConnection.readObject(TcpConnection.java:139)
... 3 more
我尝试在 Metor 中创建一个没有参数的构造函数 class 但这也没有用
Exception in thread "Client" com.esotericsoftware.kryonet.KryoNetException: Error during deserialization.
at com.esotericsoftware.kryonet.TcpConnection.readObject(TcpConnection.java:141)
at com.esotericsoftware.kryonet.Client.update(Client.java:247)
at com.esotericsoftware.kryonet.Client.run(Client.java:333)
at java.lang.Thread.run(Thread.java:745)
Caused by: com.esotericsoftware.kryo.KryoException: java.lang.IllegalArgumentException: Can not set org.newdawn.slick.geom.Shape field com.connorbrezinsky.spaceraiders.objects.Object.s to java.lang.Float
Serialization trace:
s (com.connorbrezinsky.spaceraiders.objects.Metor)
metors (com.connorbrezinsky.spaceraiders.main.Response)
at com.esotericsoftware.kryo.serializers.FieldSerializer$ObjectField.read(FieldSerializer.java:626)
at com.esotericsoftware.kryo.serializers.FieldSerializer.read(FieldSerializer.java:221)
at com.esotericsoftware.kryo.Kryo.readClassAndObject(Kryo.java:735)
at com.esotericsoftware.kryo.serializers.CollectionSerializer.read(CollectionSerializer.java:109)
at com.esotericsoftware.kryo.serializers.CollectionSerializer.read(CollectionSerializer.java:18)
at com.esotericsoftware.kryo.Kryo.readObject(Kryo.java:654)
at com.esotericsoftware.kryo.serializers.FieldSerializer$ObjectField.read(FieldSerializer.java:605)
at com.esotericsoftware.kryo.serializers.FieldSerializer.read(FieldSerializer.java:221)
at com.esotericsoftware.kryo.Kryo.readClassAndObject(Kryo.java:735)
at com.esotericsoftware.kryonet.KryoSerialization.read(KryoSerialization.java:58)
at com.esotericsoftware.kryonet.TcpConnection.readObject(TcpConnection.java:139)
... 3 more
Caused by: java.lang.IllegalArgumentException: Can not set org.newdawn.slick.geom.Shape field com.connorbrezinsky.spaceraiders.objects.Object.s to java.lang.Float
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:167)
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:171)
at sun.reflect.UnsafeObjectFieldAccessorImpl.set(UnsafeObjectFieldAccessorImpl.java:81)
at java.lang.reflect.Field.set(Field.java:764)
at com.esotericsoftware.kryo.serializers.FieldSerializer$ObjectField.read(FieldSerializer.java:619)
... 13 more
这是客户端代码
Kryo kryo = client.getKryo();
kryo.register(Request.class);
kryo.register(Response.class);
kryo.register(java.util.ArrayList.class);
kryo.register(Metor.class);
client.start();
try {
client.connect(10000, "192.168.1.130", 54555);
} catch (Exception er) {
er.printStackTrace();
}
Request request = new Request();
request.text="map_metor";
client.sendTCP(request);
client.addListener(new Listener() {
public void received(Connection connection, Object object) {
Response response = (Response) object;
System.out.println(response.metors);
}
});
这是服务器代码
public static Server server;
public static World world;
public static final int PORT = 54555;
public static void main(String[] args) throws IOException {
System.out.println("Starting server...");
server = new Server();
world = new World(2000,2000);
Kryo kryo = server.getKryo();
kryo.register(Request.class);
kryo.register(Response.class);
kryo.register(java.util.ArrayList.class);
kryo.register(Metor.class);
server.start();
server.bind(54555);
System.out.println("Server started, generating world");
world.setMaxMetors(20);
world.setMaxPlanets(10);
world.generateObjects();
server.addListener(new Listener() {
public void received(Connection connection, Object object) {
if (object instanceof Request) {
Request request = (Request) object;
System.out.println(request.text);
if (request.text.equalsIgnoreCase("map_metor")) {
Response response = new Response();
response.metors = world.metors;
connection.sendTCP(response);
}else if(request.text.equalsIgnoreCase("map_planets")) {
Response response = new Response();
response.planets = world.planets;
connection.sendTCP(response);
}
}
}
});
}
很抱歉 post,但这似乎是一个奇怪的问题,除非我完全忘记了某些事情。
编辑:
所以我得出结论,Kryonet 在序列化 slick2d Image class 时遇到问题,或者与 Kryonet 和 slick2d 存在不同的冲突。
你必须确保你的 类 中的所有变量都与你用 kryonet 发送的相同。
我正在尝试使用网络库 kryonet 创建一个多人游戏,我已经连接并发送字符串正常工作,但现在我正在尝试发送对象。我想要做的是发送一个数组列表,但它给了我这个错误。我也尝试只发送 1 个对象,但它给了我同样的错误。
Exception in thread "Client" com.esotericsoftware.kryonet.KryoNetException: Error during deserialization.
at com.esotericsoftware.kryonet.TcpConnection.readObject(TcpConnection.java:141)
at com.esotericsoftware.kryonet.Client.update(Client.java:247)
at com.esotericsoftware.kryonet.Client.run(Client.java:333)
at java.lang.Thread.run(Thread.java:745)
Caused by: com.esotericsoftware.kryo.KryoException: Class cannot be created (missing no-arg constructor): com.connorbrezinsky.spaceraiders.objects.Metor
at com.esotericsoftware.kryo.Kryo.newInstantiator(Kryo.java:1048)
at com.esotericsoftware.kryo.Kryo.newInstance(Kryo.java:1060)
at com.esotericsoftware.kryo.serializers.FieldSerializer.create(FieldSerializer.java:228)
at com.esotericsoftware.kryo.serializers.FieldSerializer.read(FieldSerializer.java:217)
at com.esotericsoftware.kryo.Kryo.readClassAndObject(Kryo.java:735)
at com.esotericsoftware.kryo.serializers.CollectionSerializer.read(CollectionSerializer.java:109)
at com.esotericsoftware.kryo.serializers.CollectionSerializer.read(CollectionSerializer.java:18)
at com.esotericsoftware.kryo.Kryo.readClassAndObject(Kryo.java:735)
at com.esotericsoftware.kryonet.KryoSerialization.read(KryoSerialization.java:58)
at com.esotericsoftware.kryonet.TcpConnection.readObject(TcpConnection.java:139)
... 3 more
我尝试在 Metor 中创建一个没有参数的构造函数 class 但这也没有用
Exception in thread "Client" com.esotericsoftware.kryonet.KryoNetException: Error during deserialization.
at com.esotericsoftware.kryonet.TcpConnection.readObject(TcpConnection.java:141)
at com.esotericsoftware.kryonet.Client.update(Client.java:247)
at com.esotericsoftware.kryonet.Client.run(Client.java:333)
at java.lang.Thread.run(Thread.java:745)
Caused by: com.esotericsoftware.kryo.KryoException: java.lang.IllegalArgumentException: Can not set org.newdawn.slick.geom.Shape field com.connorbrezinsky.spaceraiders.objects.Object.s to java.lang.Float
Serialization trace:
s (com.connorbrezinsky.spaceraiders.objects.Metor)
metors (com.connorbrezinsky.spaceraiders.main.Response)
at com.esotericsoftware.kryo.serializers.FieldSerializer$ObjectField.read(FieldSerializer.java:626)
at com.esotericsoftware.kryo.serializers.FieldSerializer.read(FieldSerializer.java:221)
at com.esotericsoftware.kryo.Kryo.readClassAndObject(Kryo.java:735)
at com.esotericsoftware.kryo.serializers.CollectionSerializer.read(CollectionSerializer.java:109)
at com.esotericsoftware.kryo.serializers.CollectionSerializer.read(CollectionSerializer.java:18)
at com.esotericsoftware.kryo.Kryo.readObject(Kryo.java:654)
at com.esotericsoftware.kryo.serializers.FieldSerializer$ObjectField.read(FieldSerializer.java:605)
at com.esotericsoftware.kryo.serializers.FieldSerializer.read(FieldSerializer.java:221)
at com.esotericsoftware.kryo.Kryo.readClassAndObject(Kryo.java:735)
at com.esotericsoftware.kryonet.KryoSerialization.read(KryoSerialization.java:58)
at com.esotericsoftware.kryonet.TcpConnection.readObject(TcpConnection.java:139)
... 3 more
Caused by: java.lang.IllegalArgumentException: Can not set org.newdawn.slick.geom.Shape field com.connorbrezinsky.spaceraiders.objects.Object.s to java.lang.Float
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:167)
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:171)
at sun.reflect.UnsafeObjectFieldAccessorImpl.set(UnsafeObjectFieldAccessorImpl.java:81)
at java.lang.reflect.Field.set(Field.java:764)
at com.esotericsoftware.kryo.serializers.FieldSerializer$ObjectField.read(FieldSerializer.java:619)
... 13 more
这是客户端代码
Kryo kryo = client.getKryo();
kryo.register(Request.class);
kryo.register(Response.class);
kryo.register(java.util.ArrayList.class);
kryo.register(Metor.class);
client.start();
try {
client.connect(10000, "192.168.1.130", 54555);
} catch (Exception er) {
er.printStackTrace();
}
Request request = new Request();
request.text="map_metor";
client.sendTCP(request);
client.addListener(new Listener() {
public void received(Connection connection, Object object) {
Response response = (Response) object;
System.out.println(response.metors);
}
});
这是服务器代码
public static Server server;
public static World world;
public static final int PORT = 54555;
public static void main(String[] args) throws IOException {
System.out.println("Starting server...");
server = new Server();
world = new World(2000,2000);
Kryo kryo = server.getKryo();
kryo.register(Request.class);
kryo.register(Response.class);
kryo.register(java.util.ArrayList.class);
kryo.register(Metor.class);
server.start();
server.bind(54555);
System.out.println("Server started, generating world");
world.setMaxMetors(20);
world.setMaxPlanets(10);
world.generateObjects();
server.addListener(new Listener() {
public void received(Connection connection, Object object) {
if (object instanceof Request) {
Request request = (Request) object;
System.out.println(request.text);
if (request.text.equalsIgnoreCase("map_metor")) {
Response response = new Response();
response.metors = world.metors;
connection.sendTCP(response);
}else if(request.text.equalsIgnoreCase("map_planets")) {
Response response = new Response();
response.planets = world.planets;
connection.sendTCP(response);
}
}
}
});
}
很抱歉 post,但这似乎是一个奇怪的问题,除非我完全忘记了某些事情。
编辑: 所以我得出结论,Kryonet 在序列化 slick2d Image class 时遇到问题,或者与 Kryonet 和 slick2d 存在不同的冲突。
你必须确保你的 类 中的所有变量都与你用 kryonet 发送的相同。