2014-01-23 85 views
0

我正在用java創建酒店預訂系統,我在保存和加載信息時遇到問題。我有一個名爲Global類存儲所有的數組:在java中序列化數組列表

import java.io.*; 
import java.util.*; 

public class Global implements Serializable { 
    public static ArrayList<Guests> guests = new ArrayList<Guests>(); 
    public static ArrayList<Reservations> reservations = new ArrayList<Reservations>(); 
    public static ArrayList<Rooms> rooms = new ArrayList<Rooms>(); 
} 

我也呼籲ToSave類:

import java.io.*; 
import java.util.*; 

public class ToSave implements Serializable { 
    public ArrayList<Guests> getGuests() { 
     return Global.guests; 
    } 

    public void setGuests(ArrayList<Guests> guests) { 
     Global.guests = guests; 
    } 

    public ArrayList<Reservations> getReservations() { 
     return Global.reservations; 
    } 

    public void setReservations(ArrayList<Reservations> reservations) { 
     Global.reservations = reservations; 
    } 

    public ArrayList<Rooms> getRooms() { 
     return Global.rooms; 
    } 

    public void setRooms(ArrayList<Rooms> rooms) { 
     Global.rooms = rooms; 
    } 

    public void save(String filename) { 
     try { 
      FileOutputStream fileOut = new FileOutputStream(filename); 
      ObjectOutputStream out = new ObjectOutputStream(fileOut); 
      out.writeObject(this); 
      out.close(); 
      fileOut.close(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

    public void load(String filename) { 
     try { 
      FileInputStream fileIn = new FileInputStream(filename); 
      ObjectInputStream in = new ObjectInputStream(fileIn); 
      ToSave save = (ToSave)in.readObject(); 
      this.setGuests(save.getGuests()); 
      this.setReservations(save.getReservations()); 
      this.setRooms(save.getRooms()); 
      in.close(); 
      fileIn.close(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    }  
} 

於是最後我有一類叫做uiMethods存儲的保存和加載按鈕:

if(clicker == save) { 
    ToSave save = new ToSave(); 
    save.setGuests(Global.guests); 
    save.setReservations(Global.reservations); 
    save.setRooms(Global.rooms); 
    save.save(filename); 
} 

if(clicker == load) { 
    ToSave save = new ToSave(); 
    save.load(filename); 
    Global.guests = save.getGuests(); 
    Global.reservations = save.getReservations(); 
    Global.rooms = save.getRooms(); 
} 

FYI這是客人的類所包含的內容:

public class Guests implements Serializable { 
    Integer id; 
    String name, surname, email, mobile, passport; 

    public Guests() { 
    } 

    public Guests(int id, String name, String surname, String mobile, String email, String passport) { 
     this.id = id; 
     this.name = name; 
     this.surname = surname; 
     this.mobile = mobile; 
     this.email = email; 
     this.passport = passport; 
    } 

    public int getId() { 
     return id; 
    } 

    public void setId(int id) { 
     this.id = id; 
    } 

    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 

    public String getSurname() { 
     return surname; 
    } 

    public void setSurname(String surname) { 
     this.surname = surname; 
    } 

    public String getMobileNo() { 
     return mobile; 
    } 

    public void setMobileNo(String mobile) { 
     this.mobile = mobile; 
    } 

    public String getEmail() { 
     return email; 
    } 

    public void setEmail(String email) { 
     this.email = email; 
    } 

    public String getPassportNo() { 
     return passport; 
    } 

    public void setPassportNo(String passport) { 
     this.passport = passport; 
    } 

    public boolean equals(int guestId) { 
     if (id == guestId) { 
      return true; 
     } else { 
      return false; 
     }   
    } 

    public Guests searchGuestById(int searchId) { 
     for (int i = 0; i < Global.guests.size(); i++) { 
      if (Global.guests.get(i).id == searchId) { 
       return Global.guests.get(i); 
      } 
     } 

     return null; 
    } 

    public void editGuest(Guests guestFound, uiMethods ui) { 
     ui.guestId.setText(Integer.toString(Global.guests.indexOf(guestFound))); 
     ui.name.setText(guestFound.name); 
     ui.surname.setText(guestFound.surname); 
     ui.mobileNo.setText(guestFound.mobile); 
     ui.email.setText(guestFound.email); 
     ui.passportNo.setText(guestFound.passport); 
    } 

    public void deleteGuest(Guests guestFound) { 
     Global.guests.remove(Global.guests.indexOf(guestFound));   
    } 

    private boolean validation(uiMethods ui) { 
     if (ui.name.getText().trim().length() == 0) { 
      JOptionPane.showMessageDialog(ui, "Name cannot be empty", "Error", JOptionPane.ERROR_MESSAGE); 
      return false; 
     } 

     if (ui.surname.getText().trim().length() == 0) { 
      JOptionPane.showMessageDialog(ui, "Surname cannot be empty", "Error", JOptionPane.ERROR_MESSAGE); 
      return false; 
     } 

     if (ui.mobileNo.getText().trim().length() == 0) { 
      JOptionPane.showMessageDialog(ui, "Mobile number cannot be empty", "Error", JOptionPane.ERROR_MESSAGE); 
      return false; 
     } 

     if (ui.email.getText().trim().length() == 0) { 
      JOptionPane.showMessageDialog(ui, "Email cannot be empty", "Error", JOptionPane.ERROR_MESSAGE); 
      return false; 
     } 

     if (ui.passportNo.getText().trim().length() == 0) { 
      JOptionPane.showMessageDialog(ui, "Passport number cannot be empty", "Error", JOptionPane.ERROR_MESSAGE); 
      return false; 
     } 

     return true; 
    } 

    public boolean detailsForm(uiMethods ui) {  
     if(this.validation(ui)) { 
      Guests guest = new Guests(); 

      guest.id = Integer.parseInt(ui.guestId.getText()); 
      guest.name = ui.name.getText(); 
      guest.surname = ui.surname.getText(); 
      guest.mobile = ui.mobileNo.getText(); 
      guest.email = ui.email.getText(); 
      guest.passport = ui.passportNo.getText(); 

      Global.guests.add(guest.id, guest); 

      return true; 
     } 

     return false; 
    } 
} 

當我加載程序它給我以下錯誤:java.io.notserializableexception

任何想法如何解決這個問題?非常感謝您的幫助。

+0

您的實體像全局和房間都是實現可序列化的接口? – kai

+0

預訂和客房是否可串行化? –

+0

是不是你嘗試序列化的全局變量?此外,在這樣的全球背景下,這是非常反面的模式。這實際上不是OOP應該如何工作的。 –

回答

0

NotSerializableException意味着系統不能序列化的特定對象的狀態。

  1. 確保完整序列化過程中的所有AGGREGATED對象都是可序列化的。
  2. 閱讀完整的日誌並跟蹤對象的名稱。
0

java.io.notserializableexception意味着您要保存的對象不是Serializable。

要保存對象的ArrayList,所以一些對象不可序列

你應該檢查:RoomsReservationsGuestsSerializable 如果不是,他們必須實現Serializable接口。