简体   繁体   English

在Java中序列化数组列表

[英]Serializing array lists in java

I am creating a Hotel Reservation System in java and I'm having a problem with saving and loading information. 我正在用Java创建一个酒店预订系统,并且在保存和加载信息时遇到问题。 I have a class called Global which stores all the arrays: 我有一个名为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>();
}   

I also have a class called ToSave: 我也有一个名为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();
        }
    }      
} 

Then finally I have a class called uiMethods that stores the save and load buttons: 最后,我有了一个名为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 This is what the Guests class consists of: 仅供参考,这是Guest类的组成部分:

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;
    }
}

When I'm loading the program it's giving me the following error: java.io.notserializableexception 加载程序时,出现以下错误:java.io.notserializableexception

Any ideas how to solve this problem? 任何想法如何解决这个问题? Your help is greatly appreciated. 非常感谢您的帮助。

Some of the classes Guests , Rooms , Reservations is not Serializable . GuestsRoomsReservations某些类别不可Serializable Or it contains a reference to an object which is not Serializable . 或者它包含对不能Serializable的对象的引用。 You need to fix that and the error will be gone. 您需要修复该错误,该错误将消失。

http://docs.oracle.com/javase/7/docs/api/java/io/Serializable.html http://docs.oracle.com/javase/7/docs/api/java/io/Serializable.html

NotSerializableException means that system is not able to serialize a specific object state. NotSerializableException表示系统无法序列化特定对象状态。

  1. Ensure all the AGGREGATED objects in the complete serialization process are Serializable. 确保完整序列化过程中的所有AGGREGATED对象都是可序列化的。
  2. Read the complete log and trace the name of object. 阅读完整的日志并跟踪对象的名称。

The java.io.notserializableexception means that the Object what you want to save is not Serializable. java.io.notserializableexception表示您要保存的对象不可序列化。

You want to save an ArrayList of Objects, so some of these Objects are not Serializable 您要保存对象的ArrayList,因此其中一些对象不可序列化

You should check that: Rooms , Reservations , Guests are Serializable If not, they must implement the Serializable interface. 您应检查: RoomsReservationsGuestsSerializable否则,它们必须实现可Serializable接口。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM