Can't create a Serializable object and convert it to a byte[]

So I don’t have much to say, the only thing is that I don’t know why this code:

import java.io.*;

void setup() {
  Foo foo = new Foo();
  
  try {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(baos);
    oos.writeObject(foo);
    oos.close();
  } catch (Exception e) {
    e.printStackTrace();
  }
}

class Foo implements Serializable {
  // Empty class
}

is giving me this exception:

java.io.NotSerializableException: sketch_191109a
	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 sketch_191109a.setup(sketch_191109a.java:27)
	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)

It’s probably a silly error, but I searched the problem and I can’t find the answer
It should work because the class is Serializable and it doesn’t reference anything unserialized, but it doesn’t so I don’t know

1 Like

You need to at least initialize the object (if i‘m not mistaken).

So this should fix the problem :

class Foo implements Serializable {
   Foo () {

   }
}

Nope, it stills gives the exception. I added the println to see if it does someting, but it does not
Code:

import java.io.*;

void setup() {
  Foo foo = new Foo();
  
  try {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(baos);
    oos.writeObject(foo);
    println(baos.toByteArray().length);
    oos.close();
  } catch (Exception e) {
    e.printStackTrace();
  }
  
  exit();
}

class Foo implements Serializable {
  Foo() {
    //
  }
}

Output:

java.io.NotSerializableException: sketch_191109a_pde
	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 sketch_191109a_pde.setup(sketch_191109a_pde.java:27)
	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)

1 Like

I came across this by @quark :

:slight_smile:

2 Likes

Ok, seem that making the class static makes it work.
The problem is that when making the class static, the processing functions give some errors;
the solution to that is passing the PApplet as an argument and storing it in class, then calling the function from the PApplet inside the class as described in the link from the previous answer

1 Like