对象未反序列化

Posted

技术标签:

【中文标题】对象未反序列化【英文标题】:Object not deserializing 【发布时间】:2013-12-06 12:33:15 【问题描述】:

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

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)

【问题讨论】:

你在 InGameActivity 的第 513 行有一个 NPE,你检查了吗? 您的 logcat 日志没有显示任何序列化或反序列化问题 第 513 行是 System.out.println 行,抱歉我忘了说! 我发现了一些额外的警告。每个 ChessPiece 都有一个位图,并且它说位图不可序列化,因此它不能序列化并且它保持为空,因此是 NPE。我只需要以某种方式使我的位图可序列化... 【参考方案1】:

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

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

【讨论】:

非常感谢,Transient 是一个非常方便的关键字:D

以上是关于对象未反序列化的主要内容,如果未能解决你的问题,请参考以下文章

带有 JSON 数据的大型 POST 请求在 ASP.NET Core 4.6.1 中未反序列化

WCF XML反序列化不填充数组

使用 SOAP 请求 C# 的 XML 序列化和反序列化

错误: throw new UnsupportedOperationException("方法未反编译

无法反序列化当前的JSON对象,为啥

使用 gson 反序列化反序列化对象的内部对象