Java 正在尝试通过套接字将arraylist从服务器发送到客户端
我试图使用ObjectInputStream/ObjectOutputStream通过套接字将ArrayList从服务器发送到客户端,但收到一个错误,即发送的对象为null。 应该发生以下情况(但事实并非如此): 1.在服务器端,从MySQL数据库填充ArrayList 2.发送到客户端并用于填充jTable 连接文件(服务器端)我认为我的问题是我的无效运行()Java 正在尝试通过套接字将arraylist从服务器发送到客户端,java,sockets,serialization,arraylist,Java,Sockets,Serialization,Arraylist,我试图使用ObjectInputStream/ObjectOutputStream通过套接字将ArrayList从服务器发送到客户端,但收到一个错误,即发送的对象为null。 应该发生以下情况(但事实并非如此): 1.在服务器端,从MySQL数据库填充ArrayList 2.发送到客户端并用于填充jTable 连接文件(服务器端)我认为我的问题是我的无效运行() “我收到一个发送的对象为null的错误。”这只能表示您发送了null。这个等式与MySQL无关,与JTable也几乎无关,但它确实与M
“我收到一个发送的对象为null的错误。”这只能表示您发送了null。这个等式与MySQL无关,与JTable也几乎无关,但它确实与MySQL有关。我知道这就是问题所在。我不知道如何解决它,这就是我在这里征求意见的原因。呃,如果你不想收到null,就不要发送null?不清楚您需要什么样的建议。在这里的这些行中,它应该发送非空的对象。但出于某种原因,它没有发送。我不知道这是为什么。这个。getTopology();this.ooos.writeObject(getTopology());不发送任何内容,或发送null而不是对象?事实上,这一切都是最不清楚的。您还没有发布实际的错误消息、堆栈跟踪或任何症状。以目前的形式不负责。
package networkclientserver;
import java.net.*;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
class ConnectionCW implements Runnable
{
private Socket s;
private ObjectOutputStream ooos;
private ObjectInputStream oois;
ResultSet rs;
ConnectionCW(Socket s)
{
this.s = s;
}
public void run()
{
try
{
ObjectOutputStream ooos = new ObjectOutputStream(this.s.getOutputStream());
ObjectInputStream oois = new ObjectInputStream(this.s.getInputStream());
this.getTopology();
this.ooos.writeObject(getTopology());
this.ooos.flush();
} catch (Exception ex)
{
Logger.getLogger(ConnectionCW.class.getName()).log(Level.SEVERE, null, ex);
}
} // end of method run*/
}
public ArrayList<ReadNet> getTopology() throws Exception
{
ArrayList<ReadNet> getTopology2 = new ArrayList<ReadNet>();
Connection conn2 = getConnection();
String query = "SELECT * FROM datatopology ";
Statement st;
st=conn2.createStatement();
rs=st.executeQuery(query);
ReadNet readNet;
while(rs.next())
{
readNet = new ReadNet(rs.getInt("id"),rs.getInt("numberNodes"),rs.getInt("numberHubs"),rs.getInt("numberSwitches"),rs.getString("topologyStructure"),rs.getString("country"),rs.getString("status"));
getTopology2.add(readNet);
}
return getTopology2;
}
public Connection getConnection() throws Exception
{
try
{
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/networkstopology";
String username = "user";
String password = "pass";
Class.forName(driver);
Connection conn = DriverManager.getConnection(url, username, password);
System.out.println("Connected to MySQL!");
return conn;
} catch (Exception e)
{
System.out.println(e);
}
return null;
}
}
private void buttonReadActionPerformed(java.awt.event.ActionEvent evt)
{
try
{
ArrayList<ReadNet> setTopology = (ArrayList<ReadNet>) ios.readObject();
DefaultTableModel model = (DefaultTableModel) topologyTable1.getModel();
Object[] row = new Object[7];
for (int i = 0; i < setTopology.size(); i++)
{
row[0] = setTopology.get(i).getId();
row[1] = setTopology.get(i).getNNodes();
row[2] = setTopology.get(i).getNHubs();
row[3] = setTopology.get(i).getNSwitches();
row[4] = setTopology.get(i).getCountry();
row[5] = setTopology.get(i).getTopology();
row[6] = setTopology.get(i).getStatus();
model.addRow(row);
}
} catch (IOException ex)
{
Logger.getLogger(ClientGUI.class.getName()).log(Level.SEVERE, null, ex);
} catch (ClassNotFoundException ex)
{
Logger.getLogger(ClientGUI.class.getName()).log(Level.SEVERE, null, ex);
}
}
package networkclientserver;
import java.io.Serializable;
class ReadNet implements Serializable
{
int id;
int nnodes;
int nhubs;
int nswitch;
String topology;
String country;
String status;
ReadNet(int id,int nodes, int hubs, int switches, String countries, String topology, String status)
{
this.id=id;
this.nnodes = nodes;
this.nhubs = hubs;
this.nswitch = switches;
this.country = countries;
this.topology = topology;
this.status = status;
}
public int getId()
{
return this.id;
}
public int getNNodes()
{
return this.nnodes;
}
public int getNHubs()
{
return this.nhubs;
}
public int getNSwitches()
{
return this.nswitch;
}
String getTopology()
{
return this.topology;
}
String getCountry()
{
return this.country;
}
String getStatus()
{
return this.status;
}
}