I understand the whole concept, however trying it on Processing 3.5.3 doenst make it work, and i feel like i am missing something, and yet after hours of searching on serialiazation i got this code
import java.io.*;
import java.util.*;
void setup() {
Test test = new Test("Pedro", "1337");
String fileName = "test.bin";
try {
ObjectOutputStream o = new ObjectOutputStream(new FileOutputStream(fileName));
o.writeObject(test);
o.close();
}
catch (FileNotFoundException e) {
System.out.println("File not found");
}
catch (IOException e) {
System.out.println("Error initializing stream");
e.printStackTrace();
}
try {
ObjectInputStream oi = new ObjectInputStream(new FileInputStream(fileName));
System.out.println(oi.readObject());
oi.close();
}
catch (FileNotFoundException e) {
System.out.println("File not found");
}
catch (IOException e) {
System.out.println("Error initializing stream");
e.printStackTrace();
}
catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
}
}
and the Test class is
import java.io.Serializable;
class Test implements Serializable {
String name;
String studentId;
Test (String n, String s) {
name = n;
studentId = s;
}
}
The problem seems to be on the "o.writeObject(test)" line on the setup function, the error is a java.io.NotSerializableException in which i made sure that the Test class did have implemented Serializable, and even then the same error comes up, that seems to be the only problem. Any help?
Edit: This is the outcome
Error initializing stream
java.io.NotSerializableException: testerino
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1184)
at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1548)
at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1509)
at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1432)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1178)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:348)
at testerino.setup(testerino.java:33)
at processing.core.PApplet.handleDraw(PApplet.java:2425)
at processing.awt.PSurfaceAWT$12.callDraw(PSurfaceAWT.java:1547)
at processing.core.PSurfaceNone$AnimationThread.run(PSurfaceNone.java:313)
Error initializing stream
java.io.WriteAbortedException: writing aborted; java.io.NotSerializableException: testerino
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1577)
at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:2287)
at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:2211)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:2069)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1573)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:431)
at testerino.setup(testerino.java:47)
at processing.core.PApplet.handleDraw(PApplet.java:2425)
at processing.awt.PSurfaceAWT$12.callDraw(PSurfaceAWT.java:1547)
at processing.core.PSurfaceNone$AnimationThread.run(PSurfaceNone.java:313)
Caused by: java.io.NotSerializableException: testerino
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1184)
at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1548)
at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1509)
at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1432)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1178)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:348)
at testerino.setup(testerino.java:33)
... 3 more
Edit 2: I have to say that the error message says "java.io.NotSerializableException: testerino", however "testerino" is the name of the project for the processing sketch, so my new question is why does it try serialize the whole project sketch?
Edit 3: I hope this is the last time i edit by adding this text, so let me explain a few problems: -I am using Processing 3.5.3, which i pointed out at the beginning of the post -This IDE is made for animation, meaning, the main "class" does not exist, meaning it uses a file called Sketch, this is the first block of code on the post, what i am trying to say is that there is no main method, it uses methods called "setup()" "draw()" just to setup the main code and a constant loop to animate - "testerino" is my project name, the sketch has that name, there are only two files in this project and its the sketch which does not let me implement Serializable and the "Test" class, now, when the error mentions testerino, let me ask why it tries serializing in the first place. - After too much research and testing, i come up with the idea that the code is perfect, however processing can't serialize Objects apart from normal variables such as Integers, String or even ArrayLists, this of course makes my earlier questions obsolete unless somebody manages to find a way to serialize Objects on Processing 3.5.3 Java
Conclusion Unless somebody figures out a way to serialize Objects in Processing i cant do much for my projects, i will have limit myself to storing normal variables in processing, I have researched for more than a day and a half on the topic and asked for advice with my Uni Lecturer, and he seems to be on the same page as me, although he is new to Processing. A good answer for this post might be if somebody finds a solution to the issue or when can state that Processing does not Serialize Objects but only variables. Thanks for your time. AZ
Final Edit: Everyhting above this point might have been a struggle to find a solution however the problem was that Serialization only works on .java class files, Processing uses .pde class formats, cahnging the file format to a .java helped on the serialization process.