Hi monkeys!! I’m relatively new to this community and I already need some help.
I’m trying to implement my first server - client game. Im following one tutorial on youtube, but when I’m trying to serialize my PlayerData.class, i’ve got the following error on console. I hope you might help me!
Grave: Uncaught exception thrown in Thread[LWJGL Renderer Thread,5,main]
java.lang.RuntimeException: Error serializing message
at com.jme3.network.base.MessageProtocol.messageToBuffer(MessageProtocol.java:81)
at com.jme3.network.base.DefaultClient.send(DefaultClient.java:237)
at com.jme3.network.base.DefaultClient.send(DefaultClient.java:207)
at mygame.ClientMain.simpleUpdate(ClientMain.java:65)
at com.jme3.app.SimpleApplication.update(SimpleApplication.java:242)
at com.jme3.system.lwjgl.LwjglAbstractDisplay.runLoop(LwjglAbstractDisplay.java:151)
at com.jme3.system.lwjgl.LwjglDisplay.runLoop(LwjglDisplay.java:185)
at com.jme3.system.lwjgl.LwjglAbstractDisplay.run(LwjglAbstractDisplay.java:228)
at java.lang.Thread.run(Thread.java:744)
Caused by: com.jme3.network.serializing.SerializerException: Error writing object for field:private mygame.PlayerData mygame.messages.PlayerDataMessage.data
at com.jme3.network.serializing.serializers.FieldSerializer.writeObject(FieldSerializer.java:180)
at com.jme3.network.serializing.Serializer.writeClassAndObject(Serializer.java:389)
at com.jme3.network.base.MessageProtocol.messageToBuffer(MessageProtocol.java:73)
… 8 more
Caused by: com.jme3.network.serializing.SerializerException: Error writing object for field:private com.jme3.math.Vector3f mygame.PlayerData.location
at com.jme3.network.serializing.serializers.FieldSerializer.writeObject(FieldSerializer.java:180)
at com.jme3.network.serializing.Serializer.writeClassAndObject(Serializer.java:389)
at com.jme3.network.serializing.serializers.FieldSerializer.writeObject(FieldSerializer.java:175)
… 10 more
Caused by: java.lang.NullPointerException
at com.jme3.network.serializing.serializers.Vector3Serializer.writeObject(Vector3Serializer.java:55)
at com.jme3.network.serializing.serializers.FieldSerializer.writeObject(FieldSerializer.java:173)
… 12 more
//@Serializable
public class PlayerData {
private Vector3f location;
private Quaternion rotation;
private Vector3f walkDirection;
//CLIENT
private int id;
private String name ="";
public PlayerData(){
location = new Vector3f(Vector3f.ZERO);
rotation = new Quaternion(Quaternion.ZERO);
walkDirection = new Vector3f(Vector3f.ZERO);}
public PlayerData(int id){
this.id = id;
location = new Vector3f(Vector3f.ZERO);
rotation = new Quaternion(Quaternion.ZERO);
walkDirection = new Vector3f(Vector3f.ZERO);
}
public PlayerData(int id, String name){
this.name = name;
this.id = id;
location = new Vector3f(Vector3f.ZERO);
rotation = new Quaternion(Quaternion.ZERO);
walkDirection = new Vector3f(Vector3f.ZERO);
}
/**
* @return the location
*/
public Vector3f getLocation() {
return location;
}
/**
* @param location the location to set
*/
public void setLocation(Vector3f location) {
this.location = location;
}
/**
* @return the rotation
*/
public Quaternion getRotation() {
return rotation;
}
/**
* @param rotation the rotation to set
*/
public void setRotation(Quaternion rotation) {
this.rotation = rotation;
}
/**
* @return the walkDirection
*/
public Vector3f getWalkDirection() {
return walkDirection;
}
/**
* @param walkDirection the walkDirection to set
*/
public void setWalkDirection(Vector3f walkDirection) {
this.walkDirection = walkDirection;
}
/**
* @return the id
*/
public int getId() {
return id;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
void setId(int id) {
this.id=id;}
Note: toString() is completely redundant there unless you actually like NPEs when the location is null.
And you should get in the habit of creating proper toString() methods for your objects in cases like this. Then you could just print playerData itself.
Maybe PlayerAppState is clearning it?
I mean… going back and forth on the forum like this is going to take forever where as finding where you are setting the field to null will take you like 2 minutes on your own. Just debug the issue… find out where you are setting it to null. Use the debugger and set a breakpoint if you want.
The problem is that fixing it silently breaks every existing game that upgrade by causing corruption of their streams. It also increases the size of data when sending Vector3f which could conceivably cause problems for some games.
So, fixing it breaks almost everyone and helps almost no one.