Reputation: 7913
I have the following Java class:
public class GetCardInfoRequest implements RequestBase, java.io.Serializable{
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;
}
@Override
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 java.io.Serializable, 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);
o.writeObject(req);
request = b.toByteArray();
The "Java.io.NotSerializableException" 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?
Upvotes: 1
Views: 5646
Reputation: 6138
Serialization of an object in Java means changing the object into some bytes. So, you can save these bytes and read the object by deserialization. Note that, this action is platform and JVM independent. So, you can serialize object in one platform and deserialize it in another platform. In summary, serialize converts object to bytes and deserialize converts bytes to object. In order to implement these actions. You need to have and object that is serializeable:
public class Coordinate implements java.io.Serializable{
int x;
int y;
Coordinate(int x, int y){
this.x = x;
this.y = y;
}
}
And you need to implement the serialize and deserialize methods:
import java.io.*;
public class SerializeDeserializeExample {
public static void main(String [] args) {
Coordinate c = new Coordinate(10,77);
//Serialize
try {
FileOutputStream fileOut =
new FileOutputStream("coordinate.ser");
ObjectOutputStream out = new ObjectOutputStream(fileOut);
out.writeObject(c);
out.close();
fileOut.close();
System.out.printf("Serialized data is saved in coordinate.ser");
}catch(IOException i) {
i.printStackTrace();
}
//Deserialize
Coordinate d = null;
try {
FileInputStream fileIn = new FileInputStream("coordinate.ser");
ObjectInputStream in = new ObjectInputStream(fileIn);
d = (Coordinate) in.readObject();
in.close();
fileIn.close();
}catch(IOException i) {
i.printStackTrace();
return;
}catch(ClassNotFoundException e) {
System.out.println("Coordinate class not found");
e.printStackTrace();
return;
}
System.out.println("Deserialized coordinate...");
System.out.println("x: " + d.x);
System.out.println("y: " + d.y);
}
}
For more information, please visit: https://github.com/m-vahidalizadeh/foundations/blob/master/src/algorithms/SerializeDeserializeExample.java.
Upvotes: 1
Reputation: 7913
Well, I feel like an idiot, but restarting Eclipse solved the problem -_-
Sorry for wasting your time guys, but I'm used to Visual Studio and it's the first time I use this IDE, probably did something I shouldn't have (it's much more confusing than VS to me I must say).
Again, thanks for the help, and sorry for wasting your time.
Upvotes: 0
Reputation: 40658
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:
Upvotes: 2