1

私はクライアントサーバーアプリケーションを持っています。クライアント側には、サーバーからテーブルを含むフレームを受信するためのボタンがあります。他のjComponents(JButton、JTextField)でフレームを渡すと正常に機能しますが、jTableでフレームを渡そうとすると、クライアント側でnull例外が発生します。

これが私のコードです:

クライアント側:

private class GetServerData extends Thread
{
    String server_msg = " ";
    Socket the_client;
    ObjectInputStream from_server;

    public GetServerData(Socket client)
    {
        the_client = client;
        try {
            from_server = new ObjectInputStream(the_client.getInputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void run() 
    {
        do {
            try {
                Object obj = from_server.readObject(); // this is the line when the exception reference to 
                                                       // when trying to read the JFRAME 
                if (obj instanceof JFrame) {
                    JFrame window = (JFrame)obj;
                    window.setVisible(true);
                    window.pack();
                }
                else {
                    server_msg = (String)obj;
                    System.out.println(server_msg);
                }
            }
            catch (ClassNotFoundException | IOException e) {
                e.printStackTrace();
            }
        }while(!server_msg.equals("bye"));
    }
}

サーバ側:

public void run(){
    while (true){
        try {
            data_from_client = (Vector)from_client.readObject();
            if (data_from_client.elementAt(0)equals("string")) {
                String s = "Hello user";
                to_client.writeObject(s);
                to_client.flush();
            }                   
            else if (data_from_client.elementAt(0).equals("table")) {
                String [][]d = {{"yoyo","jojo"},{"koko","momo"}}; 
                String []h   = {"name","best friend"};
                JTable jtable = new JTable(d,h);
                JScrollPane scroll = new JScrollPane(jtable);
                JPanel panel = new JPanel();
                panel.add(scroll,BorderLayout.CENTER);
                JFrame frame = new JFrame("Im from the server!!");
                frame.add(panel);
                to_client.writeObject(frame);
                to_client.flush();
            }
            //else if (data_from_client.elementAt(0).equals("bye")) {
            //    to_client.println("bye");
            //    to_client.flush();
            //    socket.close();
            //    socket = null;
            //}
        }
        catch(IOException | ClassNotFoundException ioe) {
            break;
            // error in reading streams from client
        }
    }
    close();
}

例外は次のとおりです。

Exception in thread "Thread-21" java.lang.NullPointerException
at java.awt.Container.readObject(Unknown Source)
at sun.reflect.GeneratedMethodAccessor6.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at java.io.ObjectStreamClass.invokeReadObject(Unknown Source)
at java.io.ObjectInputStream.readSerialData(Unknown Source)
at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readArray(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.access$300(Unknown Source)
at java.io.ObjectInputStream$GetFieldImpl.readFields(Unknown Source)
at java.io.ObjectInputStream.readFields(Unknown Source)
at java.awt.Container.readObject(Unknown Source)
at sun.reflect.GeneratedMethodAccessor6.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at java.io.ObjectStreamClass.invokeReadObject(Unknown Source)
at java.io.ObjectInputStream.readSerialData(Unknown Source)
at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readArray(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.access$300(Unknown Source)
at java.io.ObjectInputStream$GetFieldImpl.readFields(Unknown Source)
at java.io.ObjectInputStream.readFields(Unknown Source)
at java.awt.Container.readObject(Unknown Source)
at sun.reflect.GeneratedMethodAccessor6.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at java.io.ObjectStreamClass.invokeReadObject(Unknown Source)
at java.io.ObjectInputStream.readSerialData(Unknown Source)
at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readArray(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.access$300(Unknown Source)
at java.io.ObjectInputStream$GetFieldImpl.readFields(Unknown Source)
at java.io.ObjectInputStream.readFields(Unknown Source)
at java.awt.Container.readObject(Unknown Source)
at sun.reflect.GeneratedMethodAccessor6.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at java.io.ObjectStreamClass.invokeReadObject(Unknown Source)
at java.io.ObjectInputStream.readSerialData(Unknown Source)
at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readArray(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.access$300(Unknown Source)
at java.io.ObjectInputStream$GetFieldImpl.readFields(Unknown Source)
at java.io.ObjectInputStream.readFields(Unknown Source)
at java.awt.Container.readObject(Unknown Source)
at sun.reflect.GeneratedMethodAccessor6.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at java.io.ObjectStreamClass.invokeReadObject(Unknown Source)
at java.io.ObjectInputStream.readSerialData(Unknown Source)
at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readArray(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.access$300(Unknown Source)
at java.io.ObjectInputStream$GetFieldImpl.readFields(Unknown Source)
at java.io.ObjectInputStream.readFields(Unknown Source)
at java.awt.Container.readObject(Unknown Source)
at sun.reflect.GeneratedMethodAccessor6.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at java.io.ObjectStreamClass.invokeReadObject(Unknown Source)
at java.io.ObjectInputStream.readSerialData(Unknown Source)
at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readArray(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.access$300(Unknown Source)
at java.io.ObjectInputStream$GetFieldImpl.readFields(Unknown Source)
at java.io.ObjectInputStream.readFields(Unknown Source)
at java.awt.Container.readObject(Unknown Source)
at sun.reflect.GeneratedMethodAccessor6.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at java.io.ObjectStreamClass.invokeReadObject(Unknown Source)
at java.io.ObjectInputStream.readSerialData(Unknown Source)
at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
at pack.connect_to_server.ServerConnection$GetServerData.run(ServerConnection.java:92)

例外が発生するクライアント側でコメントしました。

4

1 に答える 1

2

JFrame(またはそのことについては任意のウィンドウ)を経由して渡すことSerializationは悪い考えです。私の経験では、これはシステムのネイティブピアへの接続に帰着するようです。転送されると、これは失われ、一般に多くの厄介な問題を引き起こします。私の経験では。

私は本当に明白なことを見逃している可能性があり、いくつかはこれらの制限を克服しましたが、私は離れていません。

可能であれば、代わりにデータコンテンツのみを転送する必要があります。一般的に安全で、煩雑さが大幅に軽減されます;)

于 2012-08-16T08:29:18.467 に答える