gpt4 book ai didi

java - 对象未反序列化

转载 作者:行者123 更新时间:2023-12-02 06:27:05 27 4
gpt4 key购买 nike

我有来自 Apache Commons 的此类,我用它来存储国际象棋数据。我正在保存一个 ChessPiece[][],其中 ChessPiece 是一个抽象类,每个类都有 Rook Pawn 等子类具有自己的坐标和其他数据。这是代码:

public class ObjectSerializer {

private static final Log log = LogFactory.getLog(ObjectSerializer.class);

public static String serialize(Serializable obj) throws IOException {
if (obj == null) return "";
try {
ByteArrayOutputStream serialObj = new ByteArrayOutputStream();
ObjectOutputStream objStream = new ObjectOutputStream(serialObj);
objStream.writeObject(obj);
objStream.close();
return encodeBytes(serialObj.toByteArray());
} catch (Exception e) {
throw WrappedIOException.wrap("Serialization error: " + e.getMessage(), e);
}
}

public static Object deserialize(String str) throws IOException {
if (str == null || str.length() == 0) return null;
try {
ByteArrayInputStream serialObj = new ByteArrayInputStream(decodeBytes(str));
ObjectInputStream objStream = new ObjectInputStream(serialObj);
return objStream.readObject();
} catch (Exception e) {
throw WrappedIOException.wrap("Deserialization error: " + e.getMessage(), e);
}
}

public static String encodeBytes(byte[] bytes) {
StringBuffer strBuf = new StringBuffer();

for (int i = 0; i < bytes.length; i++) {
strBuf.append((char) (((bytes[i] >> 4) & 0xF) + ((int) 'a')));
strBuf.append((char) (((bytes[i]) & 0xF) + ((int) 'a')));
}

return strBuf.toString();
}

public static byte[] decodeBytes(String str) {
byte[] bytes = new byte[str.length() / 2];
for (int i = 0; i < str.length(); i+=2) {
char c = str.charAt(i);
bytes[i/2] = (byte) ((c - 'a') << 4);
c = str.charAt(i+1);
bytes[i/2] += (c - 'a');
}
return bytes;
}

}

我正在像这样测试它,我只是序列化然后反序列化一个棋子

                String r = "";
ChessPiece c = null;
try {
r = ObjectSerializer.serialize(currentBoardState[0][0].realcopyPiece());//Returns the `ChessPiece` at the cell
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
c = (ChessPiece) ObjectSerializer.deserialize(r);
System.out.println(c.x);//NO MATTER WHAT I PRINT ITS A NULL POINTER
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

这是我的日志

12-06 07:25:52.880: E/AndroidRuntime(3888): FATAL EXCEPTION: main
12-06 07:25:52.880: E/AndroidRuntime(3888): java.lang.NullPointerException
12-06 07:25:52.880: E/AndroidRuntime(3888): at com.example.chess.InGameActivity$MyUndoButtonListener.onTouch(InGameActivity.java:513)
12-06 07:25:52.880: E/AndroidRuntime(3888): at android.view.View.dispatchTouchEvent(View.java:7379)
12-06 07:25:52.880: E/AndroidRuntime(3888): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2211)
12-06 07:25:52.880: E/AndroidRuntime(3888): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1954)
12-06 07:25:52.880: E/AndroidRuntime(3888): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2211)
12-06 07:25:52.880: E/AndroidRuntime(3888): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1954)
12-06 07:25:52.880: E/AndroidRuntime(3888): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2211)
12-06 07:25:52.880: E/AndroidRuntime(3888): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1954)
12-06 07:25:52.880: E/AndroidRuntime(3888): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2211)
12-06 07:25:52.880: E/AndroidRuntime(3888): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1954)
12-06 07:25:52.880: E/AndroidRuntime(3888): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2211)
12-06 07:25:52.880: E/AndroidRuntime(3888): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1954)
12-06 07:25:52.880: E/AndroidRuntime(3888): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1966)
12-06 07:25:52.880: E/AndroidRuntime(3888): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1418)
12-06 07:25:52.880: E/AndroidRuntime(3888): at android.app.Activity.dispatchTouchEvent(Activity.java:2424)
12-06 07:25:52.880: E/AndroidRuntime(3888): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1914)
12-06 07:25:52.880: E/AndroidRuntime(3888): at android.view.View.dispatchPointerEvent(View.java:7564)
12-06 07:25:52.880: E/AndroidRuntime(3888): at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:3883)
12-06 07:25:52.880: E/AndroidRuntime(3888): at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3778)
12-06 07:25:52.880: E/AndroidRuntime(3888): at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3379)
12-06 07:25:52.880: E/AndroidRuntime(3888): at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3429)
12-06 07:25:52.880: E/AndroidRuntime(3888): at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3398)
12-06 07:25:52.880: E/AndroidRuntime(3888): at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3483)
12-06 07:25:52.880: E/AndroidRuntime(3888): at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3406)
12-06 07:25:52.880: E/AndroidRuntime(3888): at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3540)
12-06 07:25:52.880: E/AndroidRuntime(3888): at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3379)
12-06 07:25:52.880: E/AndroidRuntime(3888): at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3429)
12-06 07:25:52.880: E/AndroidRuntime(3888): at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3398)
12-06 07:25:52.880: E/AndroidRuntime(3888): at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3406)
12-06 07:25:52.880: E/AndroidRuntime(3888): at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3379)
12-06 07:25:52.880: E/AndroidRuntime(3888): at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5419)
12-06 07:25:52.880: E/AndroidRuntime(3888): at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5399)
12-06 07:25:52.880: E/AndroidRuntime(3888): at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5370)
12-06 07:25:52.880: E/AndroidRuntime(3888): at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:5493)
12-06 07:25:52.880: E/AndroidRuntime(3888): at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:182)
12-06 07:25:52.880: E/AndroidRuntime(3888): at android.os.MessageQueue.nativePollOnce(Native Method)
12-06 07:25:52.880: E/AndroidRuntime(3888): at android.os.MessageQueue.next(MessageQueue.java:132)
12-06 07:25:52.880: E/AndroidRuntime(3888): at android.os.Looper.loop(Looper.java:124)
12-06 07:25:52.880: E/AndroidRuntime(3888): at android.app.ActivityThread.main(ActivityThread.java:5103)
12-06 07:25:52.880: E/AndroidRuntime(3888): at java.lang.reflect.Method.invokeNative(Native Method)
12-06 07:25:52.880: E/AndroidRuntime(3888): at java.lang.reflect.Method.invoke(Method.java:525)
12-06 07:25:52.880: E/AndroidRuntime(3888): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
12-06 07:25:52.880: E/AndroidRuntime(3888): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-06 07:25:52.880: E/AndroidRuntime(3888): at dalvik.system.NativeStart.main(Native Method)

最佳答案

根据用户的评论,每个棋子都有一个位图

Java 中的位图 不可序列化,请在这些位图对象之前添加一个transient 关键字,或者如果您想序列化位图,请查看下面的链接如需更多帮助
Serializing and De-Serializing android.graphics.Bitmap in Java
android how to save a bitmap - buggy code

关于java - 对象未反序列化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20424031/

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