gpt4 book ai didi

java - 我应该序列化 keyPressed 吗?

转载 作者:行者123 更新时间:2023-11-30 01:59:51 26 4
gpt4 key购买 nike

我想将一些对象保存到文件中。但我得到了java.io.NotSerializedException

这是完整的异常日志:

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)

我认为从 at main.keyPressed(main.java:79) 问题出在 main 的第 79 行,所以:

    void keyPressed() {  
if((key == 'F') || (key == 'f')){
println("Save shapes into file"); // Save shapes into file
value = 6;
fileIo = new FileOperator(0,shapes);
}
if(key == CODED){
//circle to ellipse
if((keyCode == CONTROL) && (c.centerIsSet)){
c.isItAnEllipse = false;
c.setCircleRadius(sqrt((mouseX+c.x)^2+(mouseY+c.y)^2));
shapes.add(c);
c = new Circle();
System.out.println(" Circle saved\n");
}
//Save a polygon if it is a polygon
else if((keyCode == CONTROL) && (p.savable())){
p = new Polygon();
System.out.println(" Polygon finished\n");
}
//End a polygon if it isn't a polygon yet
else if((keyCode == CONTROL) && (p.savable())){ //here is the row 79
p = new Polygon();
System.out.println(" Polygon cacelled\n");
}
//End a Catmull Rom
else if(keyCode == CONTROL){
cr = new CatmullRomManager();
System.out.println(" CatmullRom spline saved\n");
}
}
}

我有一些类,例如抽象类Shape实现Serializedclass Line extends Shape以及其他一些扩展class Shape

我正在尝试将对象保存到文件中,如下所示:

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
}
}
}

所以我不知道,但我想知道是否应该以某种方式序列化 keyPressed

或者我该如何解决这个异常?

最佳答案

序列化函数实际上没有意义。您序列化类的实例

您的错误是由于尝试序列化不可序列化的类的实例而引起的。它与未序列化的函数无关。

跟踪代码以找到不可序列化的类,然后将其标记为可序列化或停止尝试序列化它。

看起来您的错误是说 main 类(旁注:请遵循标准命名约定,类应以大写字母开头)不可序列化。也许你有一个内部类或其他什么?

关于java - 我应该序列化 keyPressed 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53244961/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com