我正在尝试通过处理制作一个彩色正方形的 3D 矩阵。它工作正常,但是当我想将它保存到序列化 DataMatrix 对象的文件中时,我得到了这个异常:java.io.NotSerializableException。Square 和 DataMatrix 都实现了 Serializable,所以我不知道是什么原因造成的。
文件管理器:
class FileManager
{
FileManager()
{}
public void saveMatrix(String path, DataMatrix dm)
{
try
{
FileOutputStream file = new FileOutputStream(path);
ObjectOutputStream output = new ObjectOutputStream(file);
output.writeObject(dm);
output.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
public DataMatrix openMatrix(String path)
{
DataMatrix dm = null;
try
{
FileInputStream file = new FileInputStream(path);
ObjectInputStream input = new ObjectInputStream(file);
Object aux = input.readObject();
input.close();
if(aux instanceof DataMatrix)
{
dm = (DataMatrix)aux;
}
}
catch (EOFException e1)
{
System.out.println ("Fin de fichero");
}
catch (Exception e2)
{
e2.printStackTrace();
}
return dm;
}
}
数据矩阵:
class DataMatrix implements Serializable
{
Square[][][] dataMatrix;
DataMatrix()
{
dataMatrix = new Square[5][32][32];
}
public void setSquare(int x, int y, int z, color c, String type)
{
dataMatrix[z][x][y].setSquare(c,type);
}
正方形:
class Square implements Serializable
{
public int x;
public int y;
public int z;
public color c;
public String type;
Square(int xx, int yy, int zz, color cc, String tt){
x = xx;
y = yy;
z = zz;
c = cc;
type = tt;
}
public void setSquare(color cc, String tt){
c = cc;
type = tt;
}
public void printSquare()
{
println(green(c) +" "+ type);
}
}
错误日志:
java.io.NotSerializableException: processing.core.PApplet$InternalEventQueue
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1164)
at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1518)
at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1483)
at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1400)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1158)
at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1518)
at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1483)
at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1400)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1158)
at java.io.ObjectOutputStream.writeArray(ObjectOutputStream.java:1346)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1154)
at java.io.ObjectOutputStream.writeArray(ObjectOutputStream.java:1346)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1154)
at java.io.ObjectOutputStream.writeArray(ObjectOutputStream.java:1346)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1154)
at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1518)
at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1483)
at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1400)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1158)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:330)
at maindatamatrix$FileManager.saveMatrix(maindatamatrix.java:105)
at maindatamatrix.setup(maindatamatrix.java:30)
at processing.core.PApplet.handleDraw(PApplet.java:2245)
at processing.core.PGraphicsJava2D.requestDraw(PGraphicsJava2D.java:243)
at processing.core.PApplet.run(PApplet.java:2141)
at java.lang.Thread.run(Thread.java:662)
java.io.WriteAbortedException: writing aborted; java.io.NotSerializableException: processing.core.PApplet$InternalEventQueue
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1332)
at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:1946)
at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1870)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1752)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1328)
at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:1946)
at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1870)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1752)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1328)
at java.io.ObjectInputStream.readArray(ObjectInputStream.java:1666)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1322)
at java.io.ObjectInputStream.readArray(ObjectInputStream.java:1666)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1322)
at java.io.ObjectInputStream.readArray(ObjectInputStream.java:1666)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1322)
at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:1946)
at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1870)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1752)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1328)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:350)
at maindatamatrix$FileManager.openMatrix(maindatamatrix.java:123)
at maindatamatrix.setup(maindatamatrix.java:33)
at processing.core.PApplet.handleDraw(PApplet.java:2245)
at processing.core.PGraphicsJava2D.requestDraw(PGraphicsJava2D.java:243)
at processing.core.PApplet.run(PApplet.java:2141)
at java.lang.Thread.run(Thread.java:662)
Caused by: java.io.NotSerializableException: processing.core.PApplet$InternalEventQueue
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1164)
at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1518)
at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1483)
at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1400)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1158)
at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1518)
at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1483)
at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1400)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1158)
at java.io.ObjectOutputStream.writeArray(ObjectOutputStream.java:1346)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1154)
at java.io.ObjectOutputStream.writeArray(ObjectOutputStream.java:1346)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1154)
at java.io.ObjectOutputStream.writeArray(ObjectOutputStream.java:1346)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1154)
at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1518)
at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1483)
at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1400)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1158)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:330)
at maindatamatrix$FileManager.saveMatrix(maindatamatrix.java:105)
at maindatamatrix.setup(maindatamatrix.java:30)
... 4 more
我认为它可能是颜色类,所以我使用瞬态修饰符来检查它。还是同样的错误。有任何想法吗?