实现Serializable后如何修复java.io.NotSerializableException

时间:2019-07-07 00:00:20

标签: java javafx serialization java-io

我试图在将客户端添加到文件列表之后将其添加到文件中,因此我已经实现了Company Class的可序列化接口以及Client Class的实现,但似乎每次都会显示异常

我尝试将新客户端添加到“公司类别”中的列表中,但是每次我尝试添加客户端时仍会部署异常

公司类别:     包装样品;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class Company implements Serializable {

    private static final long serialVersionUID = 1L;

    private static Company Instance = new Company();

    public static Company getInstance() {
        return Instance;
    }

    private ObservableList<Client>ClientList= FXCollections.observableArrayList();

    private Company() {
    }


    private Client Check_if_Client_Exist(String ClientPhoneNUmber){

        Client TempClient = null;
        for(Client Client :ClientList){
            if(Client.getPhoneNumber().equalsIgnoreCase(ClientPhoneNUmber))
                TempClient = Client;
        }
        return TempClient;
    }





    public boolean Add_New_Client_To_The_List(Client Client){

        if(Check_if_Client_Exist(Client.getPhoneNumber())==null){
            ClientList.add(Client);
            return true;
        }

        return false;
    }


    public void Store_Client_In_File () throws IOException {

        try(ObjectOutputStream ClientFile = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream("Client.dat")))){
            for(Client TempClient : ClientList){
                ClientFile.writeObject(TempClient);
            }
        }
    }


    public void Load_CLient_Data_From_File() throws IOException,ClassNotFoundException{

        boolean EndOfFile = false;
        ClientList.clear();
        try(ObjectInputStream ClientFile = new ObjectInputStream(new BufferedInputStream(new FileInputStream("Client.dat")))){
            while(!EndOfFile){
                try{
                    Client Client = (Client) ClientFile.readObject();
                    ClientList.add(Client);
                }catch(EOFException E){
                    E.printStackTrace();
                }
            }

        }
    }


    public ObservableList<Client> getClientList() {
        return ClientList;
    }
}

客户端类

  package sample;

import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;


import java.io.Serializable;

public class Client implements Serializable {

    private static final Long serialVersionUID = 1L;
    private transient SimpleStringProperty FullName = new SimpleStringProperty("");
    private transient SimpleStringProperty PhoneNumber = new SimpleStringProperty("");

public Client(){

}

    public Client(String fullName, String phoneNumber) {
      FullName.set(fullName);
      PhoneNumber.set(phoneNumber);
    }

    public String getFullName() {
        return FullName.get();
    }

    public SimpleStringProperty fullNameProperty() {
        return FullName;
    }

    public void setFullName(String fullName) {
        this.FullName.set(fullName);
    }

    public String getPhoneNumber() {
        return PhoneNumber.get();
    }

    public SimpleStringProperty phoneNumberProperty() {
        return PhoneNumber;
    }

    public void setPhoneNumber(String phoneNumber) {
        this.PhoneNumber.set(phoneNumber);
    }
}

GUI控制器所在的addClient类。

package sample;

import javafx.fxml.FXML;
import javafx.scene.control.Alert;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;

import java.io.IOException;

public class AddClient {


    @FXML
    private TextField FullName;
    @FXML
    private TextField PhoneNumber;
    @FXML
    private BorderPane AddClientBorder;




    @FXML
    public void Hnadle_The_Additionof_Client() throws IOException {

        String Fullname = FullName.getText();
        String Phonenumber = PhoneNumber.getText();
        Client Client = new Client(Fullname,Phonenumber);
        if(Company.getInstance().Add_New_Client_To_The_List(Client)){
            Company.getInstance().Store_Client_In_File();
            Alert Info = new Alert(Alert.AlertType.INFORMATION);
            Info.initOwner(AddClientBorder.getScene().getWindow());
            Info.setTitle("Done.");
            Info.setContentText("Client with "+Phonenumber+" has been succefully added !");
            Info.showAndWait();
        }else{
            Alert Error = new Alert(Alert.AlertType.ERROR);
            Error.initOwner(AddClientBorder.getScene().getWindow());
            Error.setTitle("Error");
            Error.setContentText("Client with "+Phonenumber+" already Exist");
            Error.showAndWait();
        }


    }

}

我希望它能正常工作,这似乎很合理,因为我实现了Serializable 谢谢。

0 个答案:

没有答案