[英]Java Serialization/De-serialization giving null object references
I'm working on a group project and we've run into a problem with the serialization part of our software.我正在做一个小组项目,我们遇到了软件序列化部分的问题。 We have the class protocol:我们有 class 协议:
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;
}
}
We also have the SearchResult class:我们还有 SearchResult class:
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
The problem arises when we write the Protocol class over a client-server socket connection.当我们通过客户端-服务器套接字连接编写协议 class 时,问题就出现了。 The issue is that the TableModel object in the Protocol class becomes null at some point in the serialization/de-serialization process, and nothing we have done has yet fixed this.问题是协议 class 中的 TableModel object 在序列化/反序列化过程中的某个时刻变成了 null,我们还没有做任何事情来解决这个问题。
We have so far tried:到目前为止,我们已经尝试过:
- implementing Serializable in SearchResult (I don't believe that it should be necessary due to TableModel implementing Serializable) - 在 SearchResult 中实现 Serializable(我不认为这是必要的,因为 TableModel 实现了 Serializable)
- changing the TableModel field variable to a SearchResult field variable in Protocol - 将 TableModel 字段变量更改为协议中的 SearchResult 字段变量
Any help would be most appreciated.非常感激任何的帮助。
This code correctly serializes and de-serializes the object: 此代码正确地序列化和反序列化对象:
//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());
Output: 输出:
5 5
In this case, it's something with how you write the object to the stream. 在这种情况下,与如何将对象写入流有关。
FYI: TableModel is an interface that does not implement Serializable. 仅供参考:TableModel是一个未实现Serializable的接口。 Implementing Serializable is necessary here only for Protocol. 在这里,仅对于协议,才需要实现Serializable。 If you're instead writing out the class implementing TableModel, then that one needs to implement Serializable too. 如果您要写出实现TableModel的类,那么该类也需要实现Serializable。
See the docs: 参见文档:
http://docs.oracle.com/javase/7/docs/api/javax/swing/table/TableModel.html http://docs.oracle.com/javase/7/docs/api/javax/swing/table/TableModel.html
("protocol Controller") I think you should implement and use this (“协议控制器”)我认为你应该实现并使用它
import java.io.Serializable;
import javax.swing.table.TableModel;
@Named(value = "protocolController")
@SessionScoped
public class Protocol implements Serializable
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.