1

有人请帮忙,我有一个客户端和一个服务器,我在这段代码中从客户端发送一个对象,上面写着发送数据包:

   class Data implements Serializable {
        /**
         * 
         */
        // private static final long serialVersionUID = 1L;

        String a = "ok baby";
        String b = "hi there";
    }

    public void Connect(String[] args) throws IOException {

        Socket kkSocket = null;
        PrintWriter out = null;
        ObjectOutputStream oos = null;
        BufferedReader in = null;

        try {
            kkSocket = new Socket("192.168.0.5", 1337);
            out = new PrintWriter(kkSocket.getOutputStream(), true);
            oos = new ObjectOutputStream(kkSocket.getOutputStream());
            in = new BufferedReader(new InputStreamReader(
                    kkSocket.getInputStream()));
        } catch (UnknownHostException e) {
            System.err.println("Don't know about hostname, network problem.");
            System.exit(1);
        } catch (IOException e) {
            System.err.println("Couldn't connect to server.");
            System.exit(1);
        }

        BufferedReader stdIn = new BufferedReader(new InputStreamReader(
                System.in));
        String fromServer;
        String fromUser;

        // SEND PACKET OF DATA
        oos.writeObject(new Data());
        oos.flush();
        // SEND PACKET OF DATA

此代码根本不传输,并且在尝试发送时弹出此错误,有人介意在这里帮助我吗?

java.io.NotSerializableException: java.lang.Thread
    at java.io.ObjectOutputStream.writeObject0(Unknown Source)
    at java.io.ObjectOutputStream.defaultWriteFields(Unknown Source)
    at java.io.ObjectOutputStream.writeSerialData(Unknown Source)
    at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)
    at java.io.ObjectOutputStream.writeObject0(Unknown Source)
    at java.io.ObjectOutputStream.writeArray(Unknown Source)
    at java.io.ObjectOutputStream.writeObject0(Unknown Source)
    at java.io.ObjectOutputStream.access$300(Unknown Source)
    at java.io.ObjectOutputStream$PutFieldImpl.writeFields(Unknown Source)
    at java.io.ObjectOutputStream.writeFields(Unknown Source)
    at java.awt.Container.writeObject(Unknown Source)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at java.io.ObjectStreamClass.invokeWriteObject(Unknown Source)
    at java.io.ObjectOutputStream.writeSerialData(Unknown Source)
    at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)
    at java.io.ObjectOutputStream.writeObject0(Unknown Source)
    at java.io.ObjectOutputStream.writeArray(Unknown Source)
    at java.io.ObjectOutputStream.writeObject0(Unknown Source)
    at java.io.ObjectOutputStream.access$300(Unknown Source)
    at java.io.ObjectOutputStream$PutFieldImpl.writeFields(Unknown Source)
    at java.io.ObjectOutputStream.writeFields(Unknown Source)
    at java.awt.Container.writeObject(Unknown Source)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at java.io.ObjectStreamClass.invokeWriteObject(Unknown Source)
    at java.io.ObjectOutputStream.writeSerialData(Unknown Source)
    at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)
    at java.io.ObjectOutputStream.writeObject0(Unknown Source)
    at java.io.ObjectOutputStream.writeArray(Unknown Source)
    at java.io.ObjectOutputStream.writeObject0(Unknown Source)
    at java.io.ObjectOutputStream.access$300(Unknown Source)
    at java.io.ObjectOutputStream$PutFieldImpl.writeFields(Unknown Source)
    at java.io.ObjectOutputStream.writeFields(Unknown Source)
    at java.awt.Container.writeObject(Unknown Source)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at java.io.ObjectStreamClass.invokeWriteObject(Unknown Source)
    at java.io.ObjectOutputStream.writeSerialData(Unknown Source)
    at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)
    at java.io.ObjectOutputStream.writeObject0(Unknown Source)
    at java.io.ObjectOutputStream.writeArray(Unknown Source)
    at java.io.ObjectOutputStream.writeObject0(Unknown Source)
    at java.io.ObjectOutputStream.access$300(Unknown Source)
    at java.io.ObjectOutputStream$PutFieldImpl.writeFields(Unknown Source)
    at java.io.ObjectOutputStream.writeFields(Unknown Source)
    at java.awt.Container.writeObject(Unknown Source)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at java.io.ObjectStreamClass.invokeWriteObject(Unknown Source)
    at java.io.ObjectOutputStream.writeSerialData(Unknown Source)
    at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)
    at java.io.ObjectOutputStream.writeObject0(Unknown Source)
    at java.io.ObjectOutputStream.defaultWriteFields(Unknown Source)
    at java.io.ObjectOutputStream.writeSerialData(Unknown Source)
    at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)
    at java.io.ObjectOutputStream.writeObject0(Unknown Source)
    at java.io.ObjectOutputStream.writeObject(Unknown Source)
    at citadelRPG.Main.Connect(Main.java:124)
    at citadelRPG.Main.<init>(Main.java:158)
    at citadelRPG.Main$2.run(Main.java:269)
    at java.awt.event.InvocationEvent.dispatch(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$000(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at citadelRPG.Main$2.run(Main.java:277)
    at java.awt.event.InvocationEvent.dispatch(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$000(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

我的部分来源是从 这个问题的第二个答案中使用的,我是一个 java 新手。

4

1 回答 1

2

Data 类是外部类的非静态内部类,它扩展了 Thread 或具有 Thread 类型的属性。每个非静态内部类都有对其外部对象的隐式引用。为了说明这一点,下面的代码

public class Outer extends Thread {
    class Data implements Serializable {
        String a = "ok baby";
        String b = "hi there";
    }

    ...
}

相当于以下内容:

public class Outer extends Thread
    ...
}

class Data implements Serializable {
     String a = "ok baby";
     String b = "hi there";
     Outer outer;
}

因此,当序列化 Data 的实例时,它也会尝试序列化外部对象,并且 Thread 不可序列化,它会中断。

要解决此问题,请将Data内部类设为静态,或将其设为顶级类。

于 2013-04-20T07:14:05.107 回答