FieldSerializer throws NullPointerException

hello,



I’m not absolutely sure, but I think that when a Class, that is serialized via FieldSerializer, has got an enum-field and the field is set to null, the FieldSerializer would throw a NullPointerException. Could this be true??



The EnumSerializer implements the following method:



[java]

public void writeObject(ByteBuffer buffer, Object object) throws IOException {

buffer.putInt(((Enum)object).ordinal());

}

[/java]



This would throw a NullPointerException wouldn’t it?? If the object-parameter was null?? However, I’m not a hundred percent sure, because I have added a BreakPoint at EnumSerializer.java:18. The method appears to be never called in my program. Nevertheless, the NullPointerException is only thrown when the enum-field is null.



This is the exception:


14.10.2010 18:52:15 me.codeninja.spidermonkey.serializing.serializers.FieldSerializer writeObject
WARNUNG: [FieldSerializer][???] Exception occured on writing. Maybe you've forgotten to register a class, or maybe a class member does not have a serializer.
14.10.2010 18:52:15 me.codeninja.spidermonkey.serializing.serializers.FieldSerializer writeObject
WARNUNG: [FieldSerializer][???] Exception occured on writing. Maybe you've forgotten to register a class, or maybe a class member does not have a serializer.
java.io.IOException: java.io.IOException: java.lang.NullPointerException
at me.codeninja.spidermonkey.serializing.serializers.FieldSerializer.writeObject(FieldSerializer.java:106)
at me.codeninja.spidermonkey.serializing.Serializer.writeClassAndObject(Serializer.java:230)
at me.codeninja.spidermonkey.connection.TCPConnection.send(TCPConnection.java:200)
at me.codeninja.spidermonkey.connection.TCPConnection.sendObject(TCPConnection.java:184)
at me.codeninja.spidermonkey.connection.Client.sendTCP(Client.java:183)
at fl.onlinegame.server.engine.Player.send(Player.java:55)


Thanks in advance,
Fellkneul

If an object is null, it’ll be handled in Serializer, which FieldSerializer simply uses for all the class’ fields. In other words, a null pointer should never occur there and as such I’ll look into this, thanks for the report.



Edit: Just checked the code, and I’m not so sure of this one. Can you show me your message that includes the enum and the registrations you’ve done to the Serializer?

I mean, I don’t know… but whenever I modified the enum-field which was null with “transient” everything worked fine. Or if I assigned a value. If not, this exception was thrown to me. Thank you very much for dealing with it!!

Yeah, transient fields are not sent across the network. I’ll try this out again and see if there’s a bug :slight_smile:

Oops, I haven’t read you Edit in your message before. Sorry!



Now, it is actually no subclass to Message and the class is part of a rather big project. I will send you the class that is causing the problem. It is part of an ArrayList. By debugging I have already found out, that it is only this very class that causes this exception.





[java]

package fl.onlinegame.shared.engine.objects;





import me.codeninja.spidermonkey.serializing.Serializable;

import me.codeninja.spidermonkey.serializing.serializers.FieldSerializer;





@Serializable(id = 102, serializer = FieldSerializer.class)

public class MovingEntity

{

// ENUMS

// — Enums for different states that can be set in type, employment etc.

public enum Types {

A, B, C

};

public enum Employments {

A, B, C, D

};

public enum ReferenceTypes {

A, B

};

public enum Queues {

A, B, C

};







// FIELDS

// — data

private int id;

private int owner;

private Types type;

private Employments employment;

private int reference;

private ReferenceTypes referenceType;

private float referenceX;

private float referenceY;

private Queues currQueue;

private String currAction;

private int housing;

private int target;

private int way;

private long time;

private float posX;

private float posY;

private String inventory;







// METHODS

// — Constructor

public MovingEntity(final int id, final int owner, final Types type, final Employments employment, final int reference, final ReferenceTypes referenceType, final float referenceX, final float referenceY, final Queues currQueue, final String currAction, final int housing, final int target, final int way, final long time, final float posX, final float posY, final String inventory)

{

this.id = id;

this.owner = owner;

this.type = type;

this.employment = employment;

this.reference = reference;

this.referenceType = referenceType;

this.referenceX = referenceX;

this.referenceY = referenceY;

this.currQueue = currQueue;

this.currAction = currAction;

this.housing = housing;

this.target = target;

this.way = way;

this.time = time;

this.posX = posX;

this.posY = posY;

this.inventory = inventory;

}







// — Getters & Setters

public int getId()

{

return id;

}



// … more getters n setters…



public void setInventory(final String inventory)

{

this.inventory = inventory;

}

}

[/java]





An instance of this class is created by another class in my program which is called MySQLProcessor. It would download the data from a mysql-db and save everything in an instance of MovingEntity using its constructor. In the database there is also one column called referenceType. This column can be null in the db. If the entry in the db is null, then so is the field referenceType-field in my MovingEntity instance. ONLY if it’s null, then this exception is thrown. When I adjust a value via eclipse-debugger or set the field in the class to transient, no exception is thrown.



I’m sorry, I know you’ve got better stuff to do than understanding my code and trying to solve my problems. However, I hope it’s not a stupid mistake of mine, but a bug in SpiderMonkey you will be able to fix.



Thanks in advance,

Fellkneul

BTW: I know such an object would throw an InstantiationException when “deserializing” on the other side, because of the parameters in the constructor. However, it has not come so far, yet.

There was indeed a problem, and it has been fixed now (it’s in SVN, too :)) Thanks for the report, if you still have problems, don’t hesitate to post again :slight_smile:

I still have a problem with InstantiationException.

[java]

import com.jme3.network.message.Message;

import com.jme3.network.serializing.Serializable;



@Serializable

public class Command extends Message{

public String command;



public Command(String cmd){

command = cmd;

}

}

[/java]

Messages must always have a no-arg constructor. You can leave in what you have now, but you have to have a no-arg ctor :slight_smile:

Ok, no problem. x )