在java中序列化数组列表

在java中序列化数组列表,java,serialization,arraylist,save,loading,Java,Serialization,Arraylist,Save,Loading,我正在用java创建一个酒店预订系统,但在保存和加载信息方面遇到了问题。我有一个名为Global的类,它存储所有数组: import java.io.*; import java.util.*; public class Global implements Serializable { public static ArrayList<Guests> guests = new ArrayList<Guests>(); public static Array

我正在用java创建一个酒店预订系统,但在保存和加载信息方面遇到了问题。我有一个名为Global的类,它存储所有数组:

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>();
}   
仅供参考这是来宾课程的组成部分:

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;
    }
}
公共类来宾实现可序列化{
整数id;
字符串名称、姓氏、电子邮件、手机、护照;
公众来宾(){
}
公共来宾(int-id、String-name、String-姓氏、String-mobile、String-email、String-passport){
this.id=id;
this.name=名称;
this.姓氏=姓氏;
this.mobile=mobile;
this.email=电子邮件;
这个护照=护照;
}
公共int getId(){
返回id;
}
公共无效集合id(内部id){
this.id=id;
}
公共字符串getName(){
返回名称;
}
公共void集合名(字符串名){
this.name=名称;
}
公共字符串getNames(){
返回姓氏;
}
public void setSurname(字符串姓氏){
this.姓氏=姓氏;
}
公共字符串getMobileNo(){
返回手机;
}
公共void setMobileNo(字符串移动){
this.mobile=mobile;
}
公共字符串getEmail(){
回复邮件;
}
公用电子邮件(字符串电子邮件){
this.email=电子邮件;
}
公共字符串getPassportNo(){
返回护照;
}
公共无效setPassportNo(字符串passport){
这个护照=护照;
}
公共布尔等于(int guestId){
如果(id==guestId){
返回true;
}否则{
返回false;
}        
} 
公共来宾searchGuestById(int searchId){
对于(int i=0;i
当我加载程序时,它会给我以下错误:java.io.notserializableexception


有没有办法解决这个问题?非常感谢你的帮助

一些班级
客人
房间
预订
不可
序列化
。 或者它包含对不可序列化的对象的引用。 您需要修复该问题,错误就会消失


NotSerializableException表示系统无法序列化特定的对象状态

  • 确保完整序列化过程中的所有聚合对象都是可序列化的
  • 阅读完整的日志并跟踪对象的名称

  • java.io.notserializableexception
    表示要保存的对象不可序列化

    您希望保存对象的ArrayList,因此其中一些对象不可序列化

    您应该检查:
    房间
    预订
    客人
    是否可序列化

    如果没有,他们必须实现
    可序列化接口。

    您的实体(如global和rooms)都实现了可序列化接口?预订和rooms是否可序列化?您尝试序列化的不是全局变量吗?而且,在这一点上有这样一个全球背景是非常反模式的。这真的不是OOP应该如何工作的。
    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();
    }  
    
    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;
        }
    }