![](/img/trans.png)
[英]notserializableexception for an object whose class implements Serializable
[英]NotSerializableException serializing class that implements Serializable
我在使用ObjectOutputStream#writeObject
方法序列化此類時遇到麻煩。
這是我的課:
public class InteractedObject implements Serializable{
private static final long serialVersionUID = 537798409913313981L;
protected int id;
protected WorldTile tile;
protected int option;
private long interactionTime;
public InteractedObject(int id, WorldTile tile, int option){
this.id = id;
this.tile = tile;
this.option = option;
}
public long getInteractionTime(){
return interactionTime;
}
public void setInteractionTime(long interactionTime){
this.interactionTime = interactionTime;
}
public boolean isEqualTo(Object o){
if(this == o)
return true;
InteractedObject obj = o instanceof InteractedObject ? (InteractedObject)o : null;
if(obj == null)
return false;
return obj.id == id && obj.tile.equals(tile) && obj.option == option;
}
}
WorldTile
類肯定實現了Serializable
接口,並且僅使用short
和byte
類型的字段。 我看不到為什么這會為我的類“ a.rodit.rs.InteractedObject”(頂部的類)拋出NotSerializableException
。
任何幫助或指導,為什么會發生這種情況將不勝感激。
編輯:
我的WorldTile類如下:
public class WorldTile implements Serializable {
private static final long serialVersionUID = -6567346497259686765L;
private short x, y;
private byte plane;
public WorldTile(){
this(0, 0, 0);
}
public WorldTile(int x, int y, int plane) {
this.x = (short) x;
this.y = (short) y;
this.plane = (byte) plane;
}
public final WorldTile getLocation() {
WorldTile tile = new WorldTile(x, y, plane);
return tile;
}
public WorldTile(WorldTile tile) {
this.x = tile.x;
this.y = tile.y;
this.plane = tile.plane;
}
public WorldTile(WorldTile tile, int randomize) {
this.x = (short) (tile.x + Utils.getRandom(randomize * 2) - randomize);
this.y = (short) (tile.y + Utils.getRandom(randomize * 2) - randomize);
this.plane = tile.plane;
}
public void moveLocation(int xOffset, int yOffset, int planeOffset) {
x += xOffset;
y += yOffset;
plane += planeOffset;
}
public final void setLocation(WorldTile tile) {
setLocation(tile.x, tile.y, tile.plane);
}
public final void setLocation(int x, int y, int plane) {
this.x = (short) x;
this.y = (short) y;
this.plane = (byte) plane;
}
public int getX() {
return x;
}
public int getXInRegion() {
return x & 0x3F;
}
public int getYInRegion() {
return y & 0x3F;
}
public int getY() {
return y;
}
public int getPlane() {
if (plane > 3)
return 3;
return plane;
}
public int getChunkX() {
return (x >> 3);
}
public int getChunkY() {
return (y >> 3);
}
public int getRegionX() {
return (x >> 6);
}
public int getRegionY() {
return (y >> 6);
}
public int getRegionId() {
return ((getRegionX() << 8) + getRegionY());
}
public int getLocalX(WorldTile tile, int mapSize) {
return x - 8 * (tile.getChunkX() - (Settings.MAP_SIZES[mapSize] >> 4));
}
public int getLocalY(WorldTile tile, int mapSize) {
return y - 8 * (tile.getChunkY() - (Settings.MAP_SIZES[mapSize] >> 4));
}
public int getLocalX(WorldTile tile) {
return getLocalX(tile, 0);
}
public int getLocalY(WorldTile tile) {
return getLocalY(tile, 0);
}
public int getLocalX() {
return getLocalX(this);
}
public int getLocalY() {
return getLocalY(this);
}
public int get18BitsLocationHash() {
return getRegionY() + (getRegionX() << 8) + (plane << 16);
}
public int get30BitsLocationHash() {
return y + (x << 14) + (plane << 28);
}
public boolean withinDistance(WorldTile tile, int distance) {
if (tile.plane != plane)
return false;
int deltaX = tile.x - x, deltaY = tile.y - y;
return deltaX <= distance && deltaX >= -distance && deltaY <= distance
&& deltaY >= -distance;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + plane;
result = prime * result + x;
result = prime * result + y;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
WorldTile other = (WorldTile) obj;
if (plane != other.plane)
return false;
if (x != other.x)
return false;
if (y != other.y)
return false;
return true;
}
public boolean withinDistance(WorldTile tile) {
if (tile.plane != plane)
return false;
// int deltaX = tile.x - x, deltaY = tile.y - y;
return Math.abs(tile.x - x) <= 15 && Math.abs(tile.y - y) <= 15;// deltaX
// <= 14
// &&
// deltaX
// >=
// -15
// &&
// deltaY
// <= 14
// &&
// deltaY
// >=
// -15;
}
public int getCoordFaceX(int sizeX) {
return getCoordFaceX(sizeX, -1, -1);
}
public static final int getCoordFaceX(int x, int sizeX, int sizeY,
int rotation) {
return x + ((rotation == 1 || rotation == 3 ? sizeY : sizeX) - 1) / 2;
}
public static final int getCoordFaceY(int y, int sizeX, int sizeY,
int rotation) {
return y + ((rotation == 1 || rotation == 3 ? sizeX : sizeY) - 1) / 2;
}
public int getCoordFaceX(int sizeX, int sizeY, int rotation) {
return x + ((rotation == 1 || rotation == 3 ? sizeY : sizeX) - 1) / 2;
}
public int getCoordFaceY(int sizeY) {
return getCoordFaceY(-1, sizeY, -1);
}
public int getCoordFaceY(int sizeX, int sizeY, int rotation) {
return y + ((rotation == 1 || rotation == 3 ? sizeX : sizeY) - 1) / 2;
}
}
如前所述,我認為它沒有任何問題。
僅當某些要序列化的對象未實現java.io.Serializable接口時,才引發NotSerializableException
我試圖模擬問題並復制了您的課程。 然后,我創建了一個示例WorldTile類,如下所示:
public class WorldTile implements Serializable {
short x = 4;
byte y = 1;
}
我的代碼如下,並且運行良好:
File file = new File("obj.txt");
file.createNewFile();
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file));
oos.writeObject(new InteractedObject(4, new WorldTile(), 5));
System.out.println("success");
您未能實現這一目標的唯一原因可能是您的WorldTile類。
編輯:
另一個問題可能是您是否無法運行文件的最新版本。 您是否已將所有.java文件保存在應用程序中?
您還確定已實現的Serializable接口是內嵌的java.io.Serializable
嗎?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.