2015-01-17 34 views
1

我已經做了一個JButton與netbean JFrame中,我想序列化一個對象時,我點擊了一個JButton序列化對象在一個JButton

錯誤:java.io.NotSerializableException:javax.swing.GroupLayout

我不序列化任何佈局,但它爲什麼有這個錯誤?

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {           
getValue1 a =new getValue1(); 
a.name=jTextField1.getText(); 
a.ps=jTextField2.getText(); 
a.type=jComboBox1.getSelectedItem().toString(); 

try{ 
    FileOutputStream fileOut =new FileOutputStream("C:\\employee.ser"); 
    ObjectOutputStream out = new ObjectOutputStream(fileOut); 
    out.writeObject(a); 
    out.close(); 
    fileOut.close(); 
    System.out.printf("C:\\employee.ser"); 
} 
catch(IOException i){ 
     i.printStackTrace(); 
    } 
} 

public class getValue1 implements java.io.Serializable{ 
    public String name; 
    public String ps; 
    public String type; 
} 
+0

您的getValue1可能是內部類,然後附帶類的成員;嘗試使用*靜態*內部類 –

+0

@philippelhardy你在正確的軌道上,但'靜態內在'是一個矛盾的術語。你應該說'靜態嵌套'。 – EJP

+0

Thks @philippelhardy – user3042789

回答

1

如果getValue1(這是一個不好的名字爲一類,類不是領域,名字應該看起來更像值)是另一個類的聲明(這是一個內部類)串行的一部分會嘗試序列化父類。

您應該將該類移至新的類聲明。