私はクライアントサーバーアプリケーションを持っています。クライアント側には、サーバーからテーブルを含むフレームを受信するためのボタンがあります。他の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)
例外が発生するクライアント側でコメントしました。