2017-04-17 97 views
-4

爲什麼代碼無法實現!不能讀取serlaizable文件! 它正確地寫在文件中,但不能讀取爲什麼代碼無法實現!無法讀取serlaizable文件?

enter image description here

public ArrayList<customer> read_file(){ 
ArrayList <customer>file = new ArrayList<customer>(); 
try { 
    File f= new File("file.bin"); 
    ObjectInputStream is= new ObjectInputStream (new FileInputStream("file.bin")); 
    int x=(int) f.length(); 


    while(is.available()!=0){ 
    file.add((customer)is.readObject()) ; 

    } 
} catch (FileNotFoundException ex) { 
    Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex); 
} catch (IOException | ClassNotFoundException ex) { 
    Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex); 
} 

return file ;} 
+3

此處粘貼代碼。不要給一些外部鏈接。 –

+0

請參見[什麼是堆棧跟蹤,以及如何使用它來調試我的應用程序錯誤?](http://stackoverflow.com/q/3988788/418556)&[什麼是空指針異常,以及如何修復它?](http://stackoverflow.com/q/218384/418556) –

+0

我建議,不要使用序列化,而是使用像JAXB或其他方法,序列化僅用於短期存儲對象,通常用於傳輸線...它有問題,很容易破壞 – MadProgrammer

回答

0

你的錯誤是空指針異常。您將您的客戶數組初始化爲空。我會建議使用ArrayList來代替。

+0

我嘗試但不工作 –