1

我有一个客户端-服务器应用程序,在客户端我有一个按钮可以从服务器接收一个带有表格的框架。如果我用其他jComponents(JButton,JTextField)传递框架,它工作正常,但是当我试图用jTable传递框架时,我在客户端得到空异常。

这是我的代码:

客户端:

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

通过 JFrames(或任何窗口)通过Serialization是一个坏主意,根据我的经验,这似乎归结为它与系统本机对等点的连接,一旦转移,它就会丢失并且通常会导致很多讨厌的问题,在我的经验中。

我可能会错过非常明显的东西,有些人已经克服了这些限制,但我还没有找到。

如果可以的话,您应该只传输数据内容,它通常更安全且不那么混乱;)

于 2012-08-16T08:29:18.467 回答