如何在JavaFX中将对象写入ArrayList中的文件?

时间:2015-10-31 22:03:27

标签: serialization arraylist javafx

我正在进行的项目是模拟Verizon员工登录/问卷

我让用户在TextFields中输入信息,我使用那些文本字段作为构造函数创建一个Object,(全职员工,兼职等等),然后序列化,或者将对象写入文件(data.txt) )。

不幸的是,我在Try / Catch块中遇到错误。

一直返回"无法保存数据,请联系管理员" 这是我的自定义提醒。

以下是一些有用的代码:

我的保存按钮操作

 saveBtn.setOnAction(new EventHandler<ActionEvent>(){
        @Override
            public void handle(ActionEvent e){
            System.out.println("Save Button Pressed");
           // saveData();
          //  try{

                if(fullTime.isSelected()){
                //newPerson = 

                    FullTimeEmployee employee = new FullTimeEmployee(firstNameText.getText(), lastNameText.getText(),
                    Integer.valueOf(phoneNumberText.getText()),addressText.getText(),cityText.getText(),(String)stateBox.getValue(), Integer.valueOf(zipCodeText.getText()),
                    Integer.valueOf(IDNumberText.getText()),jobFunctionText.getText(),branchText.getText(), Integer.valueOf(salaryText.getText()));


                    data.getEmployeeList().add(employee);
                    data.saveArrays();
            }

这是我的数据类(包括SaveArrays()方法):

public class Data implements Serializable{

    //ArrayLists
    private ArrayList<Employee> employeeList;
    private ArrayList<Customer> customerList;
    java.io.File file = new File("data.txt");

    public Data(){
        this.employeeList = new ArrayList<Employee>();
        this.customerList = new ArrayList<Customer>();

    }

public void saveArrays(){
        FileOutputStream fos = null;
        ObjectOutputStream output = null;

        try{

        fos = new FileOutputStream("data.txt");
        output = new ObjectOutputStream(fos);


        }
           catch(FileNotFoundException e)
        {
        System.out.println("File Was Not Found.");
        //return false;
        }
        catch(IOException e)
        {
//
        }

这是发生错误的代码的一部分。

   try{ if(output != null){
        output.writeObject(getEmployeeList());
        output.writeObject(getCustomerList());
        output.close();
        }
        }
        catch(FileNotFoundException e)
        {
            Alert alert1 = new Alert(Alert.AlertType.ERROR);
            alert1.setTitle("Unable to Find Data File");
            alert1.setHeaderText(null);
            alert1.setContentText("Unable to Find Data File"
            + ". \nPlease Contact your administrator");
            alert1.showAndWait();

        }
        catch(IOException e)
        {
            Alert alert = new Alert(Alert.AlertType.ERROR);
            alert.setTitle("Unable to Save data file");
            alert.setHeaderText(null);
            alert.setContentText("Unable to save data file"
            + ". \nPlease Contact your administrator");
            alert.showAndWait();

        }
     }


    public void setEmployeeList(ArrayList<Employee> employeeList){
    this.employeeList = employeeList;}

    public ArrayList<Employee> getEmployeeList(){
   return employeeList; 
    }
    public void setCustomerList(ArrayList<Customer> CustomerList){
    this.customerList = customerList;
    }

    public ArrayList<Customer> getCustomerList(){
   return customerList; 
    }

另外..我的data.txt位于我的Verizon Project文件夹的SRC文件夹中

这是我收到的StackTrace错误:

Save Button Pressed
java.io.NotSerializableException: verizonproject.FullTimeEmployee
    at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1184)
    at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:348)
    at java.util.ArrayList.writeObject(ArrayList.java:762)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at java.io.ObjectStreamClass.invokeWriteObject(ObjectStreamClass.java:1028)
    at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1496)
    at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1432)
    at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1178)
    at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:348)
    at verizonproject.Data.saveArrays(Data.java:69)
    at verizonproject.VerizonProject$2.handle(VerizonProject.java:159)
    at verizonproject.VerizonProject$2.handle(VerizonProject.java:143)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
    at javafx.event.Event.fireEvent(Event.java:198)
    at javafx.scene.Node.fireEvent(Node.java:8411)
    at javafx.scene.control.Button.fire(Button.java:185)
    at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182)
    at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:96)
    at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89)
    at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
    at javafx.event.Event.fireEvent(Event.java:198)
    at javafx.scene.Scene$MouseHandler.process(Scene.java:3757)
    at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
    at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
    at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:352)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:275)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$355(GlassViewEventHandler.java:388)
    at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:387)
    at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
    at com.sun.glass.ui.View.notifyMouse(View.java:937)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$149(WinApplication.java:191)
    at java.lang.Thread.run(Thread.java:745)

0 个答案:

没有答案
相关问题