2017-04-10 129 views
1

我想从我的程序中使用事件处理程序保存数据。 下面是我在控制器代码:在Java中保存处理程序

private class SaveMenuHandler implements EventHandler<ActionEvent> { 
    public void handle(ActionEvent e) { 
     try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("Student.dat"));) { 

      oos.writeObject(model); 
      oos.flush(); 

      alertDialogBuilder("Information Dialog", "Save success", "Student Profile saved to StudentProfile.dat"); 

     } 
     catch (IOException ioExcep){ 
      System.out.println("Error saving"); 
     } 

    } 
} 

此代码只是给了我“错误保存”,在控制台

任何帮助表示赞赏。

这里的错误:

Error saving 
java.lang.Exception 
    at controller.ProfileController$SaveMenuHandler.handle(ProfileController.java:194) 
    at controller.ProfileController$SaveMenuHandler.handle(ProfileController.java:1) 
    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.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58) 
    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.control.MenuItem.fire(MenuItem.java:462) 
    at com.sun.javafx.scene.control.skin.ContextMenuContent$MenuItemContainer.doSelect(ContextMenuContent.java:1405) 
    at com.sun.javafx.scene.control.skin.ContextMenuContent$MenuItemContainer.lambda$createChildren$343(ContextMenuContent.java:1358) 
    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.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:380) 
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:294) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$354(GlassViewEventHandler.java:416) 
    at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389) 
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:415) 
    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$148(WinApplication.java:191) 
    at java.lang.Thread.run(Unknown Source) 
+0

感谢您的评论。你可以更具体一点我无视吗? – SPatel

+1

你当前的代码当然不会抛出这个异常。 'Exception'不是'IOException'的子类,这意味着你不打印'ioExcep'的堆栈跟踪,但可能只是为了让一个堆栈跟踪打印到控制台而创建的一些例外... – fabian

+0

什么是ProfileController第194行和第1行? – Sedrick

回答

0

你改变你的catch块的代码,所以你可以看到确切的错误信息。你的代码应该像这样。再次运行您的程序并发布它将生成的错误消息:

private class SaveMenuHandler implements EventHandler<ActionEvent> { 
    public void handle(ActionEvent e) { 
     try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("Student.dat"));) { 

      oos.writeObject(model); 
      oos.flush(); 

      alertDialogBuilder("Information Dialog", "Save success", "Student Profile saved to StudentProfile.dat"); 

     } 
     catch (IOException ioExcep){ 
      System.out.println("Error saving"); 
      //Print the complete error message 
      ioExcep.printStackTrace(); 
     } 

    } 
} 
+0

你能发布完整的错误消息吗?这将帮助我看到什么是错的。并且请记住:错误消息不只是一个错误。这是解决软件问题的关键。 – Max

+0

我编辑了错误的原始帖子。我自己看透了他们,但不明白什么是错的。 – SPatel

+0

@fabian你是对的。我没有看到他的异常被命名为“ioException”。最后一行代码应该是ioExcep.printStackTrace(); – Max