不可序列化的异常 - 组布局

时间:2014-11-30 21:40:01

标签: java swing user-interface serialization

所以我得到了这个例外,我一直在玩弄 例外全日研究等。

##Error 
    java.io.NotSerializableException: javax.swing.GroupLayout
    at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1184)

此后还有更多,但如果需要,这显然是最相关的 我可以添加更多的错误甚至更具体的代码,但我有 我已经放弃了解决这个问题的一点。

##Code

    private void saveData()
    {
       //Create Dialog
        JFileChooser objFileDialogue = new JFileChooser();
        //Create Dialogue result with default cancel action
        int intDialogResult = JFileChooser.CANCEL_OPTION;
        //Show Dialog
        intDialogResult = objFileDialogue.showSaveDialog(this);
        //Test dialog result
        if (intDialogResult == JFileChooser.APPROVE_OPTION)
        {
            //Get the file name to use from the dialog
            File objFile = objFileDialogue.getSelectedFile();
            //Declare output stream
            //Create a Stream writer to the file
            try (ObjectOutputStream objOut = new ObjectOutputStream(
                            new BufferedOutputStream(
                            new FileOutputStream(objFile))))
            {
                //Write the warehouse object (stockList) to the file
                objOut.writeObject(newCustomerList);
                JOptionPane.showMessageDialog(this,
                        "Warehouse data saved.",
                        "Save completed.", JOptionPane.INFORMATION_MESSAGE);
            } catch (IOException ex)
            {
                Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                JOptionPane.showMessageDialog(this,
                        "Error saving data model.",
                        "File save error: " + ex.getMessage(), JOptionPane.ERROR_MESSAGE);
            }
        } else
        {
            JOptionPane.showMessageDialog(this,
                    "Save operation cancelled",
                    "Operation aborted", JOptionPane.ERROR_MESSAGE);
        }
    }
}

此处还有正在序列化的CustomerList类

public class CustomerList implements IObserver, ISubject, Serializable {

private ArrayList<Customer> customerList;

private ISubject subjectDelegate;

/**
 * Default constructor builds an empty customer list object with no customers
 * yet registered to use the delivery services. Required for serialisation.
 */
public CustomerList()
{
    this.customerList = new ArrayList<>();
    this.subjectDelegate = new ISubjectImpl();
}


/**
 * This method adds a new customer to the list of customers that have
 * registered to use the Fast Courier Service.
 * @param newCustomer
 */
public void addCustomer(Customer newCustomer)
{
    if(null != newCustomer){
        if(null == this.customerList){
            this.customerList = new ArrayList<>();
        }
        if(this.customerList.add(newCustomer)){
            newCustomer.registerObserver(this);
            this.notifyObservers();
        }

    }
}

/**
 * Customers are stored in a zero based array, this method removes the customer
 * at the given index
 * @param index - The zero based index of the customer to remove from the customer list
 * @return - The Customer object removed from the list or NULL if no customer was removed.
 * @throws IndexOutOfBoundsException - If index is negative or greater than or equal to
 * the size of the customer list.
 */
public Customer removeCustomerAt(int index) throws IndexOutOfBoundsException {
    Customer result = null;
    if(null != this.customerList && 0 < this.customerList.size()){
        if(index >= 0 && index < this.customerList.size()){
            result = this.customerList.get(index);
            this.customerList.remove(index); 
            this.notifyObservers();
        } else {
            throw new IndexOutOfBoundsException("No customer in customer list at index " + index);
        }
    }
    return result;
}

public ArrayList<Customer> getCustomers()
{
    ArrayList<Customer> arlResult = new ArrayList<>();
    for (Customer currItem : this.customerList)
    {
        arlResult.add(currItem);
    }
    return arlResult;
}
/**
 * This method retrieves an array of customer names (full names) providing a complete
 * list of all registered customers.
 * @return - An array of String objects where each element contains a 
 * registered customers full name
 */
public String[] getAllNames(){
    String[] result = null;
    if(null != this.customerList && 0 < this.customerList.size())
    {
        result = new String[this.customerList.size()];
        for(int i = 0; i < this.customerList.size(); i++){
            result[i] = this.customerList.get(i).getFullName();
        }
    } 
    else 
    {
        result = new String[0];
    }
    return result;
}

/**
 * This method retrieves the customer object at the specified zero based index
 * in the CustomerList
 * @param index - The zero based index of the customer to retrieve from the customer list
 * @return - A Customer object at the specified index position. 
 */
public Customer getCustomerAt(int index){
    return this.customerList.get(index);
}

/**
 * This method retrieves the total number of customers that are registered to use
 * the Fast Courier Service.
 * @return - An int being the total number of registered customers.
 */
public int getSize()
{
    return this.customerList.size();
}

@Override
public void update() {
    this.notifyObservers();
}

@Override
public Boolean registerObserver(IObserver o) {
    return this.subjectDelegate.registerObserver(o);
}

@Override
public Boolean removeObserver(IObserver o) {
    return this.subjectDelegate.removeObserver(o);
}

@Override
public void notifyObservers() {
    this.subjectDelegate.notifyObservers();
}

}

1 个答案:

答案 0 :(得分:1)

您遇到this问题。我的猜测是你通过registerObserver()方法注册包含javax.swing.GroupLayout的UI组件。请通过 transient 从序列化中排除 subjectDelegate