-3

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.

Bleiserman ADN
  • 141
  • 1
  • 1
  • 6
  • 2
    Please post the whole stacktrace – nortontgueno Mar 11 '19 at 00:44
  • What do you mean? This code is just a test that i made, if i can't serialize the Test class how can i make it serialize? Edit: Di you mean the code seems not compilable? If its that sorry to confuse you but Processing compiles the code like this – Bleiserman ADN Mar 11 '19 at 01:41
  • 2
    Are you working from the Processing editor? Is your `Test` class in a new tab? Where is `testerino` coming from? I don't see that anywhere in the code you posted. – Kevin Workman Mar 11 '19 at 02:10
  • 1
    It is not the `Test` class that isn't `Serializable` here. Read the error message. – user207421 Mar 11 '19 at 04:19
  • I apologise for my wording, "testerino" is the name of the whole project, there is no class with that name appart from the main sketch which would be the block of code with try/catches – Bleiserman ADN Mar 11 '19 at 09:20
  • when you call o.writeObject(test);, test is in fact --> testerino. So you are trying to serialize that testerino class (which we cannot see) – aran Mar 11 '19 at 11:21
  • **How are you defining the Test class?** Is it in its own tab, or in the same tab as the rest of your code? – Kevin Workman Mar 11 '19 at 16:05

1 Answers1

0

Put the Test class in its own tab (if you haven't already) and (re)name this tab name.java.

micycle
  • 3,328
  • 14
  • 33