Master_T Master_T - 11 months ago 46
Java Question

Serializing an object in Java

I have the following Java class:

public class GetCardInfoRequest implements RequestBase,{

public String CardID;
public String CardUniqueID;
public String who;
public String pass;

public GetCardInfoRequest(){}
public GetCardInfoRequest(String id, String uniqueid, String who, String pass){
CardID = id;
CardUniqueID = uniqueid;
this.who = who;
this.pass = pass;

public RequestType getRequestType() {
return RequestType.GetCardInfo;

public String getCardID() {
return CardID;

public String getCardUniqueID() {
return CardUniqueID;

and I'm having problems with serialization. I've been reading tutorials about java serialization, but all of them seem to imply that making an object serializable is as easy as simply implementing the "java.IO.Serializable" interface, with the only caveat of not having non-serializable fields in the class.

As you can see here, I implement, and all my fields are simple Strings (note: the "RequestBase" interface I implement just says the class must have the getRequestType method, nothing else).
But none the less, when I try to serialize an instance of the class to a byte array, like this:

ByteArrayOutputStream b = new ByteArrayOutputStream();
ObjectOutputStream o = new ObjectOutputStream(b);
request = b.toByteArray();

The "" is raised. (at the 3rd line)

I'm sure it's something trivial, but since all the tutorials are so generic I don't understand what's wrong with this very simple code. Can anyone help?

Answer Source

The NotSerializableException states that it is only thrown when a class does not implement Serializable. The message should also be the name of the class that cannot be serialised. Possible problems are:

  • You are not passing the object you think you are passing (what is the message on the exception?)
  • You are running with an out of date GetCardInfoRequest class file (try a clean build)