Should I serialize keyPressed?










1














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?










share|improve this question























  • 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
















1














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?










share|improve this question























  • 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














1












1








1







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?










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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

















  • 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













1 Answer
1






active

oldest

votes


















2














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?






share|improve this answer






















  • 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










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















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

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














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?






share|improve this answer






















  • 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















2














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?






share|improve this answer






















  • 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













2












2








2






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?






share|improve this answer














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?







share|improve this answer














share|improve this answer



share|improve this answer








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
















  • 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

















draft saved

draft discarded
















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Top Tejano songwriter Luis Silva dead of heart attack at 64

政党

天津地下鉄3号線