我正在开展小组项目,但我们遇到了软件序列化部分的问题。我们有类协议:
import java.io.Serializable;
import javax.swing.table.TableModel;
public class Protocol implements Serializable{
private static final long serialVersionUID = 3474882407641871963L;
private int state;//0 = logged out
//1=logged in waiting for search
//2= search query sent
private String output;
private TableModel searchResults;
public Protocol(int state){
this.output = "";
this.state = 0;
this.searchResults = null;
}
public void setState(int s) {
this.state = s;
}
public void setOutput(String o) {
this.output = o;
}
public void setSearchResults(TableModel results) {
this.searchResults = results;
}
public String getOutput() {
return this.output;
}
public int getState(){
return this.state;
}
public TableModel getSearchResults(){
return this.searchResults;
}
}
我们还有SearchResult类:
import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import javax.swing.event.TableModelListener; import javax.swing.table.TableModel; /** * An implementation of the TableModel interface that utilises a List of DataRow objects to * store the data. * * @version 13/03/2014 * @author Jake Darby * * @see DataRow * @see TableModel */ public class SearchResult implements TableModel { private final List DATA; private static final int NUM_COLUMNS = 8; private final int NUM_ROWS; /** * Constructor that generates a SearchResult object from a set of Results of a query to the 'products' table * * @param data The results of an SQL query of the 'products' table * @throws SQLException * * @see ResultSet */ public SearchResult (ResultSet data) throws SQLException { super(); DATA = DataRow.generate(data); NUM_ROWS = DATA.size(); } /** * Constructor creates an empty TableModel */ public SearchResult() { super(); DATA = new ArrayList(); NUM_ROWS = 0; } @Override public int getRowCount() { return NUM_ROWS; } @Override public int getColumnCount() { return NUM_COLUMNS; } @Override public String getColumnName(int columnIndex) { switch(columnIndex) { case 0: return "ID"; case 1: return "Name"; case 2: return "Price"; case 3: return "Pack Quantity"; case 4: return "Quantity in Stock"; case 5: return "VAT"; case 6: return "Order Date"; case 7: return "Expiry Date"; default: return null; } } @Override public Class getColumnClass(int columnIndex) { switch(columnIndex) { case 0: case 3: case 4: return Integer.class; case 1: case 2: case 6: case 7: return String.class; case 5: return Boolean.class; default: return null; } } @Override public boolean isCellEditable(int rowIndex, int columnIndex) { return false; } @Override public Object getValueAt(int rowIndex, int columnIndex) { DataRow row = DATA.get(rowIndex); switch(columnIndex) { case -1: return row.getPrice(); case 0: return row.getID(); case 1: return row.getName(); case 2: return String.format("£%d.%02d", row.getPrice()/100, row.getPrice()%100); case 3: return row.getPackQuantity(); case 4: return row.getQuantity(); case 5: return row.getVAT(); case 6: return row.getOrderDate() == null ? "N/A" : row.getOrderDate(); case 7: return row.getExpiryDate() == null ? "N/A" : row.getExpiryDate(); default: return null; } } @Override public void setValueAt(Object aValue, int rowIndex, int columnIndex) {/*No implementation*/} @Override public void addTableModelListener(TableModelListener l) {/*No implementation*/} @Override public void removeTableModelListener(TableModelListener l) {/*No implementation*/} @Override public String toString() { String result = ""; for (int i = 0; i
当我们通过客户端 - 服务器套接字连接编写Protocol类时,会出现问题。问题是Protocol类中的TableModel对象在序列化/反序列化过程中的某个时刻变为null,我们所做的一切都没有解决这个问题。
我们到目前为止尝试过:
- 在SearchResult中实现Serializable(我不相信由于TableModel实现Serializable而应该是必要的)
- 将TableModel字段变量更改为Protocol
中的SearchResult字段变量非常感谢任何帮助。
答案 0 :(得分:0)
此代码正确序列化和反序列化对象:
//write out the object
FileOutputStream fos = new FileOutputStream("out.myobj");
ObjectOutputStream oos = new ObjectOutputStream(fos);
Protocol p = new Protocol(4);
p.setState(5);
oos.writeObject(p);
oos.close();
//read it in
FileInputStream fis = new FileInputStream("out.myobj");
ObjectInputStream ois = new ObjectInputStream(fis);
Protocol x = (Protocol)ois.readObject();
ois.close();
System.out.println(x.getState());
输出:
5
在这种情况下,它与您将对象写入流的方式有关。
仅供参考:TableModel是不实现Serializable的接口。这里只为Protocol实现Serializable 必要。如果您正在编写实现TableModel的类,那么那个也需要实现Serializable。
参见文档:
http://docs.oracle.com/javase/7/docs/api/javax/swing/table/TableModel.html