我应该序列化keyPressed吗?

时间:2018-11-11 01:09:13

标签: java serialization file-io processing keypress

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

这是完整的异常日志:

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)的问题主要在第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");
        }
      }
    }

我有一些课程,例如abstract class Shape implements Serializableclass 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

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

1 个答案:

答案 0 :(得分:2)

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

您的错误是由于尝试序列化不可序列化的类的实例而引起的。与功能不被序列化无关。

浏览代码以查找不可序列化的类,然后将其标记为可序列化或停止尝试对其进行序列化。

您的错误似乎是说main类(不能遵循标准命名约定,类应以大写字母开头)是不可序列化的。也许你有一个内部阶级之类的东西?