什么在这里抛出IOException?

时间:2019-01-16 14:01:25

标签: java oop model-view-controller exception-handling ioexception

我试图从GUI以及模型类的arraylist中获取数据,然后将它们放入另一个arraylist中。每次代码运行时,它都能正常工作,但是当我关闭应用程序并运行序列化程序代码时,将抛出IOException。

我从模型类访问的唯一内容是一个数组列表,但是我正在其他类中访问此数组列表,但是我没有问题。

最初,我在模型类中添加了要添加的arraylist,我认为那是引发异常的原因,因此我将其移至另一个类,但是仍在引发异常。

我已经尝试了表单上其余的事件处理程序,但这是唯一导致异常的事件处理程序。

以下是导致异常发生的代码:

private void btnRequestAppointmentActionPerformed(java.awt.event.ActionEvent evt) {                                                      
    ArrayList<IAccountStrategy> observers;
    observers = model.getObservers();

    IAccountStrategy xx;

    xx = observers.get(accountIndex);     

    ArrayList<Appointments> appointment;
    appointment = xx.getAppointments();     
    Appointments app = new Appointments();

    setAppointmentDates(appointment, app);
    setAppointmentPatient(appointment, app);               
    xx.getAppointments().add(app);

    JOptionPane.showMessageDialog(null, "Appointment Requested. A Secretary will check the request shortly");
}         

它正在调用的类:

 public void setAppointmentDates(ArrayList<Appointments> appointment, Appointments app){
    String doctorsName = null;
    String sDate;
    Date appointmentDate = null;
    Date formattedDate = null;
    SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");

    try{
    doctorsName = cboDoctor.getSelectedItem().toString();
    sDate = cboDate.getSelectedItem().toString();

    appointmentDate = sdf.parse(sDate);
    formattedDate = sdf.parse(sdf.format(appointmentDate));
    }
    catch(ParseException ex){
        System.out.println("ParseException has been caught");
    }

    app.setAppointmentDate(appointmentDate);
    app.setPreferredDoctor(doctorsName);
}           

 public void setAppointmentPatient(ArrayList<Appointments> appointment, Appointments app){
    ArrayList<IAccountStrategy> observers;
    observers = model.getObservers();

    IAccountStrategy xx;        
    xx = observers.get(accountIndex);

    app.setPatientID(xx.getAccountID());
    app.setPatientName(xx.getFirstname() +" "+ xx.getLastname());
    app.setPatientAge(xx.getAge());
    app.setPatientSex(xx.getSex());
} 

这是我序列化的代码,它适用于我所有其他类:

   private void formWindowClosing(java.awt.event.WindowEvent evt) {                                   
    String filename = "accounts.txt";

    try{
    FileOutputStream file = new FileOutputStream(filename);
    ObjectOutputStream out = new ObjectOutputStream(file);

    out.writeObject(model);

    out.close();
    file.close();
    }
    catch(IOException ex){
        System.out.println("IOException has been caught");
    }
}         

这是堆栈跟踪:

java.io.NotSerializableException: patientmanagementsystemv2.items.Appointments
    at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1184)
    at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:348)
    at java.util.ArrayList.writeObject(ArrayList.java:766)
    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:498)
    at java.io.ObjectStreamClass.invokeWriteObject(ObjectStreamClass.java:1140)
    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.defaultWriteFields(ObjectOutputStream.java:1548)
    at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1509)
    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 java.util.ArrayList.writeObject(ArrayList.java:766)
    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:498)
    at java.io.ObjectStreamClass.invokeWriteObject(ObjectStreamClass.java:1140)
    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.defaultWriteFields(ObjectOutputStream.java:1548)
    at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1509)
    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 patientmanagementsystemv2.gui.PatientHomePage.formWindowClosing(PatientHomePage.java:294)
    at patientmanagementsystemv2.gui.PatientHomePage.access$000(PatientHomePage.java:28)
    at patientmanagementsystemv2.gui.PatientHomePage$1.windowClosing(PatientHomePage.java:133)
    at java.awt.Window.processWindowEvent(Window.java:2054)
    at javax.swing.JFrame.processWindowEvent(JFrame.java:305)
    at java.awt.Window.processEvent(Window.java:2013)
    at java.awt.Component.dispatchEventImpl(Component.java:4889)
    at java.awt.Container.dispatchEventImpl(Container.java:2297)
    at java.awt.Window.dispatchEventImpl(Window.java:2746)
    at java.awt.Component.dispatchEvent(Component.java:4711)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:760)
    at java.awt.EventQueue.access$500(EventQueue.java:97)
    at java.awt.EventQueue$3.run(EventQueue.java:709)
    at java.awt.EventQueue$3.run(EventQueue.java:703)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:74)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:84)
    at java.awt.EventQueue$4.run(EventQueue.java:733)
    at java.awt.EventQueue$4.run(EventQueue.java:731)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:74)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:730)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:205)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)                              

我试图在今晚完成此程序,但已经尝试修复了几个小时。任何帮助都将受到感激。

1 个答案:

答案 0 :(得分:2)

Stacktrace

就像评论中的@Stultuske一样,您可以在stacktrace中找到原因

查找策略

使用包装名称查找第一个条目。在您的情况下,请查找patientmanagementsystemv2.gui

java.io.NotSerializableException: patientmanagementsystemv2.items.Appointments
    ...
    at patientmanagementsystemv2.gui.PatientHomePage.formWindowClosing(PatientHomePage.java:294)
    at patientmanagementsystemv2.gui.PatientHomePage.access$000(PatientHomePage.java:28)
    at patientmanagementsystemv2.gui.PatientHomePage$1.windowClosing(PatientHomePage.java:133)
    ...

在堆栈跟踪的顶部,您可以看到NotSerializableException处有一个patientmanagementsystemv2.gui.PatientHomePage.formWindowClosing(PatientHomePage.java:294

问题

  

java.io.NotSerializableException: patientmanagementsystemv2.items.Appointments

引用类型Appointments似乎未实现Serializable,并且会引发错误,因为您尝试使用formWindowClosingout.writeObject(model)的方法Appointments中序列化{{1}的实例}。

修改Appointments

如果您拥有该课程,只需编写

public class Appointments implements Serializable {
    // ...
}

但是您期望一个IOException

使用catch(IOException ex)可以捕获IOException及其所有子类,包括NotSerializableException