来自 ObjectInputStream 的 NullPointerException

NullPointerException from ObjectInputStream

当我尝试通过 Socket 通过 ObjectInputStream 获得 Object 时,我得到了 NullPointerException。我正在使用服务器通过 TransferObject 向客户端 (android phone) 发送信息。我知道服务器正在发送 Object 因为当它发送错误时它被读取正常并且没有抛出异常。所有发送的 类 都是可序列化的,包含一个 serialVersionID,并且包含在两个项目中。

代码:

TransferObject data = null;

try {
    Socket s = new Socket();                                        
    s.connect(new InetSocketAddress(HOST, PORT), 10000);
    ObjectOutputStream oos = new ObjectOutputStream(s.getOutputStream());
    ObjectInputStream ois = new ObjectInputStream(s.getInputStream());

    send.setUser(useremail);
    send.setPassword(password);

    oos.writeObject(send);
    oos.flush();

    Object inputObject = ois.readObject();   //Error happens here.
    data = (TransferObject) inputObject; 

    s.close();
    return data;
}

这里是 TransferObject:

public class TransferObject implements Serializable {

    private static final long serialVersionUID = *******************L;  //blanked this out

    public String command;
    public String args;
    public Object[] objects;

    private String password;
    private String user;

    public TransferObject() {
    }

    public TransferObject(String command, String args, Object[] objects) {
        this.objects = objects;
        this.args = args;
        this.command = command;
    }

    public String getPassword() {
        return this.password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getUser() {
        return this.user;
    }

    public void setUser(String user) {
        this.user = user;
    }
}

最后,可怕的日志:

07-20 14:38:16.044: E/AndroidRuntime(2451): FATAL EXCEPTION: AsyncTask #1
07-20 14:38:16.044: E/AndroidRuntime(2451): Process: com.********************.**********.timecardmanager, PID: 2451
07-20 14:38:16.044: E/AndroidRuntime(2451): java.lang.RuntimeException: An error occured while executing doInBackground()
07-20 14:38:16.044: E/AndroidRuntime(2451):     at android.os.AsyncTask.done(AsyncTask.java:304)
07-20 14:38:16.044: E/AndroidRuntime(2451):     at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
07-20 14:38:16.044: E/AndroidRuntime(2451):     at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
07-20 14:38:16.044: E/AndroidRuntime(2451):     at java.util.concurrent.FutureTask.run(FutureTask.java:242)
07-20 14:38:16.044: E/AndroidRuntime(2451):     at android.os.AsyncTask$SerialExecutor.run(AsyncTask.java:231)
07-20 14:38:16.044: E/AndroidRuntime(2451):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
07-20 14:38:16.044: E/AndroidRuntime(2451):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
07-20 14:38:16.044: E/AndroidRuntime(2451):     at java.lang.Thread.run(Thread.java:818)
07-20 14:38:16.044: E/AndroidRuntime(2451): Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.Class.isProxy()' on a null object reference
07-20 14:38:16.044: E/AndroidRuntime(2451):     at java.lang.reflect.Proxy.isProxyClass(Proxy.java:261)
07-20 14:38:16.044: E/AndroidRuntime(2451):     at java.io.ObjectStreamClass.resolveProperties(ObjectStreamClass.java:1016)
07-20 14:38:16.044: E/AndroidRuntime(2451):     at java.io.ObjectStreamClass.isEnum(ObjectStreamClass.java:1040)
07-20 14:38:16.044: E/AndroidRuntime(2451):     at java.io.ObjectStreamClass.checkAndGetTcObjectClass(ObjectStreamClass.java:1341)
07-20 14:38:16.044: E/AndroidRuntime(2451):     at java.io.ObjectInputStream.readNewObject(ObjectInputStream.java:1788)
07-20 14:38:16.044: E/AndroidRuntime(2451):     at java.io.ObjectInputStream.readContent(ObjectInputStream.java:707)
07-20 14:38:16.044: E/AndroidRuntime(2451):     at java.io.ObjectInputStream.discardData(ObjectInputStream.java:636)
07-20 14:38:16.044: E/AndroidRuntime(2451):     at java.io.ObjectInputStream.readObjectForClass(ObjectInputStream.java:1348)
07-20 14:38:16.044: E/AndroidRuntime(2451):     at java.io.ObjectInputStream.readHierarchy(ObjectInputStream.java:1242)
07-20 14:38:16.044: E/AndroidRuntime(2451):     at java.io.ObjectInputStream.readNewObject(ObjectInputStream.java:1835)
07-20 14:38:16.044: E/AndroidRuntime(2451):     at java.io.ObjectInputStream.readNonPrimitiveContent(ObjectInputStream.java:761)
07-20 14:38:16.044: E/AndroidRuntime(2451):     at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1983)
07-20 14:38:16.044: E/AndroidRuntime(2451):     at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1940)
07-20 14:38:16.044: E/AndroidRuntime(2451):     at java.io.ObjectInputStream.readFieldValues(ObjectInputStream.java:1113)
07-20 14:38:16.044: E/AndroidRuntime(2451):     at java.io.ObjectInputStream.defaultReadObject(ObjectInputStream.java:454)
07-20 14:38:16.044: E/AndroidRuntime(2451):     at java.io.ObjectInputStream.readObjectForClass(ObjectInputStream.java:1345)
07-20 14:38:16.044: E/AndroidRuntime(2451):     at java.io.ObjectInputStream.readHierarchy(ObjectInputStream.java:1242)
07-20 14:38:16.044: E/AndroidRuntime(2451):     at java.io.ObjectInputStream.readNewObject(ObjectInputStream.java:1835)
07-20 14:38:16.044: E/AndroidRuntime(2451):     at java.io.ObjectInputStream.readNonPrimitiveContent(ObjectInputStream.java:761)
07-20 14:38:16.044: E/AndroidRuntime(2451):     at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1983)
07-20 14:38:16.044: E/AndroidRuntime(2451):     at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1940)
07-20 14:38:16.044: E/AndroidRuntime(2451):     at java.io.ObjectInputStream.readNewArray(ObjectInputStream.java:1488)
07-20 14:38:16.044: E/AndroidRuntime(2451):     at java.io.ObjectInputStream.readNonPrimitiveContent(ObjectInputStream.java:759)
07-20 14:38:16.044: E/AndroidRuntime(2451):     at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1983)
07-20 14:38:16.044: E/AndroidRuntime(2451):     at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1940)
07-20 14:38:16.044: E/AndroidRuntime(2451):     at java.io.ObjectInputStream.readFieldValues(ObjectInputStream.java:1113)
07-20 14:38:16.044: E/AndroidRuntime(2451):     at java.io.ObjectInputStream.defaultReadObject(ObjectInputStream.java:454)
07-20 14:38:16.044: E/AndroidRuntime(2451):     at java.io.ObjectInputStream.readObjectForClass(ObjectInputStream.java:1345)
07-20 14:38:16.044: E/AndroidRuntime(2451):     at java.io.ObjectInputStream.readHierarchy(ObjectInputStream.java:1242)
07-20 14:38:16.044: E/AndroidRuntime(2451):     at java.io.ObjectInputStream.readNewObject(ObjectInputStream.java:1835)
07-20 14:38:16.044: E/AndroidRuntime(2451):     at java.io.ObjectInputStream.readNonPrimitiveContent(ObjectInputStream.java:761)
07-20 14:38:16.044: E/AndroidRuntime(2451):     at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1983)
07-20 14:38:16.044: E/AndroidRuntime(2451):     at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1940)
07-20 14:38:16.044: E/AndroidRuntime(2451):     at com.********************.**********.timecardmanager.MainActivity.sendToServer(MainActivity.java:158)
07-20 14:38:16.044: E/AndroidRuntime(2451):     at com.********************.**********.timecardmanager.TimecardFragment$GetCardsTask.doInBackground(TimecardFragment.java:81)
07-20 14:38:16.044: E/AndroidRuntime(2451):     at com.********************.**********.timecardmanager.TimecardFragment$GetCardsTask.doInBackground(TimecardFragment.java:1)
07-20 14:38:16.044: E/AndroidRuntime(2451):     at android.os.AsyncTask.call(AsyncTask.java:292)
07-20 14:38:16.044: E/AndroidRuntime(2451):     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
07-20 14:38:16.044: E/AndroidRuntime(2451):     ... 4 more

提前致谢。

编辑:服务器端。 运行 每次新客户端连接到新线程时。

try
    {
        ObjectOutputStream oos = new ObjectOutputStream(soc.getOutputStream());
        ObjectInputStream ois = new ObjectInputStream(soc.getInputStream());

        TransferObject obj = (TransferObject) ois.readObject();

        switch(obj.command)
        {
            case "REQ":
                JavaTimecard[] javaCards = BullhornTools.getJavaCards(obj.getUser(), obj.getPassword());

                if(javaCards == null)
                {
                    obj.command = "ERR";
                    obj.args = BullhornTools.getError();
                    obj.setUser("");
                    obj.setPassword("");

                    oos.writeObject(obj);
                    oos.flush();
                }
                else
                {
                    obj.command = "AKK";
                    obj.args = "";
                    obj.objects = new Object[javaCards.length];
                    obj.setUser("");
                    obj.setPassword("");

                    for(int i = 0; i < javaCards.length; i++)
                    {
                        obj.objects[i] = javaCards[i];
                    }

                    oos.writeObject(obj);
                    oos.flush();
                }

                break;



            case "SAVE":
                //TODO: Implement later
                break;
        }

        soc.close();
    }
    catch (IOException e) 
    {
        e.printStackTrace();
    } 
    catch (ClassNotFoundException e) 
    {
        e.printStackTrace();
    }

我尝试通过序列化、存储在文件中并读回它来测试它。效果很好,因此我将假设它是一个 android 东西。正如塞尔文所说,刚刚使用 JSON.

对于任何可能不够清晰的地方,我深表歉意。