0

我在 JavaFX 中创建了一个程序和一个具有两个日期和时间字段的寄存器,这些字段在注册时填写。使用带有注释的休眠进行映射,当我提交日期和时间时会生成此错误并将保存:

java.lang.RuntimeException: java.lang.IllegalArgumentException

at br.upf.ads.daoo.global.action.AbstractAction.actionPerformed(AbstractAction.java:63)

at br.upf.ads.daoo.global.controller.AbstractController.handle(AbstractController.java:156)

at br.upf.ads.daoo.global.controller.AbstractController.handle(AbstractController.java:31)

at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:69)

at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:217)

at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:170)

at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:38)

at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:37)

at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)

at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35)

at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)

at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35)

at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)

at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:53)

at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:28)

at javafx.event.Event.fireEvent(Event.java:171)

at javafx.scene.Node.fireEvent(Node.java:6863)

at javafx.scene.control.Button.fire(Button.java:179)

at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:193)

at com.sun.javafx.scene.control.skin.SkinBase$4.handle(SkinBase.java:336)

at com.sun.javafx.scene.control.skin.SkinBase$4.handle(SkinBase.java:329)

at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:64)

at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:217)

at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:170)

at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:38)

at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:37)

at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)

at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35)

at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)

at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35)

at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)

at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35)

at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)

at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:53)

at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:33)

at javafx.event.Event.fireEvent(Event.java:171)

at javafx.scene.Scene$MouseHandler.process(Scene.java:3328)

at javafx.scene.Scene$MouseHandler.process(Scene.java:3168)

at javafx.scene.Scene$MouseHandler.access$1900(Scene.java:3123)

at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1563)

at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2265)

at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:250)

at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:173)

at java.security.AccessController.doPrivileged(Native Method)

at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:292)

at com.sun.glass.ui.View.handleMouseEvent(View.java:528)

at com.sun.glass.ui.View.notifyMouse(View.java:922)

at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)

at com.sun.glass.ui.win.WinApplication.access$100(WinApplication.java:29)

at com.sun.glass.ui.win.WinApplication$3$1.run(WinApplication.java:73)

at java.lang.Thread.run(Thread.java:722)

引起:java.lang.IllegalArgumentException

at java.util.Date.parse(Date.java:615)

at java.util.Date.<init>(Date.java:272)

at br.upf.ads.daoo.ui.CompraIncluirView.loadCompraFromPanel(CompraIncluirView.java:179)

at br.upf.ads.daoo.ui.CompraIncluirView.getCompra(CompraIncluirView.java:205)

at br.upf.ads.daoo.controller.CompraIncluirController$4.conditional(CompraIncluirController.java:58)

at br.upf.ads.daoo.global.action.ConditionalAction.action(ConditionalAction.java:51)

at br.upf.ads.daoo.global.action.AbstractAction.actionPerformed(AbstractAction.java:58)

... 50 more

可能是什么错误?下面是一些我认为是错误的代码:

private Compra loadCompraFromPanel() {
    Integer id = null;
    try {
        id = Integer.parseInt(tfId.getText());
    } catch (Exception nex) {
    }    
     Date data = null;
    if (!tfData.getText().trim().isEmpty()) {
       data= new Date(tfData.getText());        
    }

     Float valor = null;
    try {
        valor = Float.parseFloat(tfValor.getText());
    } catch (Exception nex) {
    }

     Date horario = null;
    if (!tfHorario.getText().trim().isEmpty()) {
        horario = new Date(tfHorario.getText());  
    }
    Passageiro passageiro = cbPassageiro.getValue();
    Capital capital = cbCapital.getValue();


    return new Compra(id, data, valor, horario, passageiro, capital);
}
4

2 回答 2

1

您收到错误是因为new Date(String s)无法解析传递给的值( tfData.getText()tfHorario.getText())。但是,您应该使用 aDateFormat而不是已弃用的Date构造函数。例如:

DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
Date horario = null;
try {
  horario = df.parse(tfHorario.getText());
} catch (ParseException e) {
  // The user didn't provide a string in the correct format, show an error or something
}

也可以看看:SimpleDateFormat

于 2013-06-16T00:37:37.273 回答
0

您得到 anIllegalArgumentException因为您将tfData.getText()(a string) 作为 (a ) 的构造函数data传递Date

data= new Date(tfData.getText());

该类的构造函数Date是:

Date()
Date(int year, int month, int date) //Deprecated
Date(int year, int month, int date, int hrs, int min) //Deprecated
Date(int year, int month, int date, int hrs, int min, int sec) //Deprecated
Date(long date)
Date(String s) //Deprecated

显然,String返回的 from对于(不推荐使用的)构造函数tfData.getText()来说不是有效的格式。Date(String s)

于 2013-06-16T00:08:17.377 回答