Should I serialize keyPressed?
I want to save some objects into a file. but I got java.io.NotSerializableException
.
This is the full exception log:
java.io.NotSerializableException: main
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 main$FileOperator.<init>(main.java:484)
at main.keyPressed(main.java:79)
at processing.core.PApplet.keyPressed(PApplet.java:3056)
at processing.core.PApplet.handleKeyEvent(PApplet.java:2931)
at processing.core.PApplet.dequeueEvents(PApplet.java:2602)
at processing.core.PApplet.handleDraw(PApplet.java:2440)
at processing.awt.PSurfaceAWT$12.callDraw(PSurfaceAWT.java:1557)
at processing.core.PSurfaceNone$AnimationThread.run(PSurfaceNone.java:313)
And I think from at main.keyPressed(main.java:79)
the problem is in main at row 79, so:
void keyPressed()
And I have some class, for example the abstract class Shape implements Serializable
and class Line extends Shape
and some others what extends class Shape
.
I am trying to save the objects into file like this:
import java.io.*;
import java.util.Random;
class FileOperator
FileOperator(int io, ArrayList<Shape> shapes)
if(io == 0)
try
Random rand = new Random();
rand.nextInt(40);
String filename = "draw" + rand + ".txt";
FileOutputStream f = new FileOutputStream(new File(filename));
ObjectOutputStream o = new ObjectOutputStream(f);
for(Shape s:shapes)
o.writeObject(s);
o.close();
f.close();
catch (FileNotFoundException e)
System.out.println("ERROR: File not found");
catch (NotSerializableException e)
println("ERROR: trying to serialize: ");
e.printStackTrace();
catch (IOException e)
println("ERROR: initializing stream");
e.printStackTrace();
else
//read
So I don't know but I am wondering whether I should serialize keyPressed
somehow?
Or how can I resolve this exception?
java serialization file-io processing keypress
add a comment |Â
I want to save some objects into a file. but I got java.io.NotSerializableException
.
This is the full exception log:
java.io.NotSerializableException: main
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 main$FileOperator.<init>(main.java:484)
at main.keyPressed(main.java:79)
at processing.core.PApplet.keyPressed(PApplet.java:3056)
at processing.core.PApplet.handleKeyEvent(PApplet.java:2931)
at processing.core.PApplet.dequeueEvents(PApplet.java:2602)
at processing.core.PApplet.handleDraw(PApplet.java:2440)
at processing.awt.PSurfaceAWT$12.callDraw(PSurfaceAWT.java:1557)
at processing.core.PSurfaceNone$AnimationThread.run(PSurfaceNone.java:313)
And I think from at main.keyPressed(main.java:79)
the problem is in main at row 79, so:
void keyPressed()
And I have some class, for example the abstract class Shape implements Serializable
and class Line extends Shape
and some others what extends class Shape
.
I am trying to save the objects into file like this:
import java.io.*;
import java.util.Random;
class FileOperator
FileOperator(int io, ArrayList<Shape> shapes)
if(io == 0)
try
Random rand = new Random();
rand.nextInt(40);
String filename = "draw" + rand + ".txt";
FileOutputStream f = new FileOutputStream(new File(filename));
ObjectOutputStream o = new ObjectOutputStream(f);
for(Shape s:shapes)
o.writeObject(s);
o.close();
f.close();
catch (FileNotFoundException e)
System.out.println("ERROR: File not found");
catch (NotSerializableException e)
println("ERROR: trying to serialize: ");
e.printStackTrace();
catch (IOException e)
println("ERROR: initializing stream");
e.printStackTrace();
else
//read
So I don't know but I am wondering whether I should serialize keyPressed
somehow?
Or how can I resolve this exception?
java serialization file-io processing keypress
Which line of code is line 79? What is line 484? Can you please post a Minimal, Complete, and Verifiable example?
â Kevin Workman
Nov 11 at 1:13
@KevinWorkman as I wrote the 79 is:else if((keyCode == CONTROL) && (p.savable())){ //here is the row 79
and I don't have row 484.
â Gábor
Nov 11 at 1:31
Personal opinion, but I would generally avoid object serialisation in most cases, and instead use either JSON parsing or XML parsing (JAXB) instead - it gives you control over HOW the parsing is done
â MadProgrammer
Nov 11 at 2:41
add a comment |Â
I want to save some objects into a file. but I got java.io.NotSerializableException
.
This is the full exception log:
java.io.NotSerializableException: main
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 main$FileOperator.<init>(main.java:484)
at main.keyPressed(main.java:79)
at processing.core.PApplet.keyPressed(PApplet.java:3056)
at processing.core.PApplet.handleKeyEvent(PApplet.java:2931)
at processing.core.PApplet.dequeueEvents(PApplet.java:2602)
at processing.core.PApplet.handleDraw(PApplet.java:2440)
at processing.awt.PSurfaceAWT$12.callDraw(PSurfaceAWT.java:1557)
at processing.core.PSurfaceNone$AnimationThread.run(PSurfaceNone.java:313)
And I think from at main.keyPressed(main.java:79)
the problem is in main at row 79, so:
void keyPressed()
And I have some class, for example the abstract class Shape implements Serializable
and class Line extends Shape
and some others what extends class Shape
.
I am trying to save the objects into file like this:
import java.io.*;
import java.util.Random;
class FileOperator
FileOperator(int io, ArrayList<Shape> shapes)
if(io == 0)
try
Random rand = new Random();
rand.nextInt(40);
String filename = "draw" + rand + ".txt";
FileOutputStream f = new FileOutputStream(new File(filename));
ObjectOutputStream o = new ObjectOutputStream(f);
for(Shape s:shapes)
o.writeObject(s);
o.close();
f.close();
catch (FileNotFoundException e)
System.out.println("ERROR: File not found");
catch (NotSerializableException e)
println("ERROR: trying to serialize: ");
e.printStackTrace();
catch (IOException e)
println("ERROR: initializing stream");
e.printStackTrace();
else
//read
So I don't know but I am wondering whether I should serialize keyPressed
somehow?
Or how can I resolve this exception?
java serialization file-io processing keypress
I want to save some objects into a file. but I got java.io.NotSerializableException
.
This is the full exception log:
java.io.NotSerializableException: main
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 main$FileOperator.<init>(main.java:484)
at main.keyPressed(main.java:79)
at processing.core.PApplet.keyPressed(PApplet.java:3056)
at processing.core.PApplet.handleKeyEvent(PApplet.java:2931)
at processing.core.PApplet.dequeueEvents(PApplet.java:2602)
at processing.core.PApplet.handleDraw(PApplet.java:2440)
at processing.awt.PSurfaceAWT$12.callDraw(PSurfaceAWT.java:1557)
at processing.core.PSurfaceNone$AnimationThread.run(PSurfaceNone.java:313)
And I think from at main.keyPressed(main.java:79)
the problem is in main at row 79, so:
void keyPressed()
And I have some class, for example the abstract class Shape implements Serializable
and class Line extends Shape
and some others what extends class Shape
.
I am trying to save the objects into file like this:
import java.io.*;
import java.util.Random;
class FileOperator
FileOperator(int io, ArrayList<Shape> shapes)
if(io == 0)
try
Random rand = new Random();
rand.nextInt(40);
String filename = "draw" + rand + ".txt";
FileOutputStream f = new FileOutputStream(new File(filename));
ObjectOutputStream o = new ObjectOutputStream(f);
for(Shape s:shapes)
o.writeObject(s);
o.close();
f.close();
catch (FileNotFoundException e)
System.out.println("ERROR: File not found");
catch (NotSerializableException e)
println("ERROR: trying to serialize: ");
e.printStackTrace();
catch (IOException e)
println("ERROR: initializing stream");
e.printStackTrace();
else
//read
So I don't know but I am wondering whether I should serialize keyPressed
somehow?
Or how can I resolve this exception?
java serialization file-io processing keypress
java serialization file-io processing keypress
edited Nov 11 at 1:20
Kevin Workman
33.4k53969
33.4k53969
asked Nov 11 at 1:09
Gábor
604
604
Which line of code is line 79? What is line 484? Can you please post a Minimal, Complete, and Verifiable example?
â Kevin Workman
Nov 11 at 1:13
@KevinWorkman as I wrote the 79 is:else if((keyCode == CONTROL) && (p.savable())){ //here is the row 79
and I don't have row 484.
â Gábor
Nov 11 at 1:31
Personal opinion, but I would generally avoid object serialisation in most cases, and instead use either JSON parsing or XML parsing (JAXB) instead - it gives you control over HOW the parsing is done
â MadProgrammer
Nov 11 at 2:41
add a comment |Â
Which line of code is line 79? What is line 484? Can you please post a Minimal, Complete, and Verifiable example?
â Kevin Workman
Nov 11 at 1:13
@KevinWorkman as I wrote the 79 is:else if((keyCode == CONTROL) && (p.savable())){ //here is the row 79
and I don't have row 484.
â Gábor
Nov 11 at 1:31
Personal opinion, but I would generally avoid object serialisation in most cases, and instead use either JSON parsing or XML parsing (JAXB) instead - it gives you control over HOW the parsing is done
â MadProgrammer
Nov 11 at 2:41
Which line of code is line 79? What is line 484? Can you please post a Minimal, Complete, and Verifiable example?
â Kevin Workman
Nov 11 at 1:13
Which line of code is line 79? What is line 484? Can you please post a Minimal, Complete, and Verifiable example?
â Kevin Workman
Nov 11 at 1:13
@KevinWorkman as I wrote the 79 is:
else if((keyCode == CONTROL) && (p.savable())){ //here is the row 79
and I don't have row 484.â Gábor
Nov 11 at 1:31
@KevinWorkman as I wrote the 79 is:
else if((keyCode == CONTROL) && (p.savable())){ //here is the row 79
and I don't have row 484.â Gábor
Nov 11 at 1:31
Personal opinion, but I would generally avoid object serialisation in most cases, and instead use either JSON parsing or XML parsing (JAXB) instead - it gives you control over HOW the parsing is done
â MadProgrammer
Nov 11 at 2:41
Personal opinion, but I would generally avoid object serialisation in most cases, and instead use either JSON parsing or XML parsing (JAXB) instead - it gives you control over HOW the parsing is done
â MadProgrammer
Nov 11 at 2:41
add a comment |Â
1 Answer
1
active
oldest
votes
It doesn't really make sense to serialize a function. You serialize instances of a class.
Your error is caused by trying to serialize an instance of a class that's not serializeable. It has nothing to do with a function not being serialized.
Trace through your code to find the class that's not serializeable, and then either mark it as serializeable or stop trying to serialize it.
It looks like your error is saying that the main
class (side note: please follow standard naming conventions, classes should start with an upper-case letter) is not serializeable. Maybe you have an inner class or something?
ok, then how to save objects to file without serialize?
â Gábor
Nov 11 at 1:35
@Gábor I'm not saying you can't use serialization. I'm saying you need to track down which part of what you're saving is not serializable. But there are a ton of ways to save data to file- JSON, XML, properties, your own custom format, etc. Start with something simple and get that working first. Get a simple serialization example working. Then if you have a question you can post a Minimal, Complete, and Verifiable example along with a more specific technical question. Good luck.
â Kevin Workman
Nov 11 at 1:42
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
It doesn't really make sense to serialize a function. You serialize instances of a class.
Your error is caused by trying to serialize an instance of a class that's not serializeable. It has nothing to do with a function not being serialized.
Trace through your code to find the class that's not serializeable, and then either mark it as serializeable or stop trying to serialize it.
It looks like your error is saying that the main
class (side note: please follow standard naming conventions, classes should start with an upper-case letter) is not serializeable. Maybe you have an inner class or something?
ok, then how to save objects to file without serialize?
â Gábor
Nov 11 at 1:35
@Gábor I'm not saying you can't use serialization. I'm saying you need to track down which part of what you're saving is not serializable. But there are a ton of ways to save data to file- JSON, XML, properties, your own custom format, etc. Start with something simple and get that working first. Get a simple serialization example working. Then if you have a question you can post a Minimal, Complete, and Verifiable example along with a more specific technical question. Good luck.
â Kevin Workman
Nov 11 at 1:42
add a comment |Â
It doesn't really make sense to serialize a function. You serialize instances of a class.
Your error is caused by trying to serialize an instance of a class that's not serializeable. It has nothing to do with a function not being serialized.
Trace through your code to find the class that's not serializeable, and then either mark it as serializeable or stop trying to serialize it.
It looks like your error is saying that the main
class (side note: please follow standard naming conventions, classes should start with an upper-case letter) is not serializeable. Maybe you have an inner class or something?
ok, then how to save objects to file without serialize?
â Gábor
Nov 11 at 1:35
@Gábor I'm not saying you can't use serialization. I'm saying you need to track down which part of what you're saving is not serializable. But there are a ton of ways to save data to file- JSON, XML, properties, your own custom format, etc. Start with something simple and get that working first. Get a simple serialization example working. Then if you have a question you can post a Minimal, Complete, and Verifiable example along with a more specific technical question. Good luck.
â Kevin Workman
Nov 11 at 1:42
add a comment |Â
It doesn't really make sense to serialize a function. You serialize instances of a class.
Your error is caused by trying to serialize an instance of a class that's not serializeable. It has nothing to do with a function not being serialized.
Trace through your code to find the class that's not serializeable, and then either mark it as serializeable or stop trying to serialize it.
It looks like your error is saying that the main
class (side note: please follow standard naming conventions, classes should start with an upper-case letter) is not serializeable. Maybe you have an inner class or something?
It doesn't really make sense to serialize a function. You serialize instances of a class.
Your error is caused by trying to serialize an instance of a class that's not serializeable. It has nothing to do with a function not being serialized.
Trace through your code to find the class that's not serializeable, and then either mark it as serializeable or stop trying to serialize it.
It looks like your error is saying that the main
class (side note: please follow standard naming conventions, classes should start with an upper-case letter) is not serializeable. Maybe you have an inner class or something?
edited Nov 11 at 1:32
answered Nov 11 at 1:15
Kevin Workman
33.4k53969
33.4k53969
ok, then how to save objects to file without serialize?
â Gábor
Nov 11 at 1:35
@Gábor I'm not saying you can't use serialization. I'm saying you need to track down which part of what you're saving is not serializable. But there are a ton of ways to save data to file- JSON, XML, properties, your own custom format, etc. Start with something simple and get that working first. Get a simple serialization example working. Then if you have a question you can post a Minimal, Complete, and Verifiable example along with a more specific technical question. Good luck.
â Kevin Workman
Nov 11 at 1:42
add a comment |Â
ok, then how to save objects to file without serialize?
â Gábor
Nov 11 at 1:35
@Gábor I'm not saying you can't use serialization. I'm saying you need to track down which part of what you're saving is not serializable. But there are a ton of ways to save data to file- JSON, XML, properties, your own custom format, etc. Start with something simple and get that working first. Get a simple serialization example working. Then if you have a question you can post a Minimal, Complete, and Verifiable example along with a more specific technical question. Good luck.
â Kevin Workman
Nov 11 at 1:42
ok, then how to save objects to file without serialize?
â Gábor
Nov 11 at 1:35
ok, then how to save objects to file without serialize?
â Gábor
Nov 11 at 1:35
@Gábor I'm not saying you can't use serialization. I'm saying you need to track down which part of what you're saving is not serializable. But there are a ton of ways to save data to file- JSON, XML, properties, your own custom format, etc. Start with something simple and get that working first. Get a simple serialization example working. Then if you have a question you can post a Minimal, Complete, and Verifiable example along with a more specific technical question. Good luck.
â Kevin Workman
Nov 11 at 1:42
@Gábor I'm not saying you can't use serialization. I'm saying you need to track down which part of what you're saving is not serializable. But there are a ton of ways to save data to file- JSON, XML, properties, your own custom format, etc. Start with something simple and get that working first. Get a simple serialization example working. Then if you have a question you can post a Minimal, Complete, and Verifiable example along with a more specific technical question. Good luck.
â Kevin Workman
Nov 11 at 1:42
add a comment |Â
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid â¦
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid â¦
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53244961%2fshould-i-serialize-keypressed%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Which line of code is line 79? What is line 484? Can you please post a Minimal, Complete, and Verifiable example?
â Kevin Workman
Nov 11 at 1:13
@KevinWorkman as I wrote the 79 is:
else if((keyCode == CONTROL) && (p.savable())){ //here is the row 79
and I don't have row 484.â Gábor
Nov 11 at 1:31
Personal opinion, but I would generally avoid object serialisation in most cases, and instead use either JSON parsing or XML parsing (JAXB) instead - it gives you control over HOW the parsing is done
â MadProgrammer
Nov 11 at 2:41