【问题标题】:Uncatchable exception thrown by calling Stage.show()调用 Stage.show() 引发的无法捕获的异常
【发布时间】:2020-03-23 18:15:23
【问题描述】:

编辑:我很幸运,James_D 在下面的 cmets 中很快发现并解释了这一现象。现在找到了使用 PropertyValueFactory 的替代方法(适用于 Groovy 用户),我想我会添加自己的答案,以帮助任何不幸的人在未来对此感到困惑。


规格 Java:11。操作系统:Linux Mint 18.3。 所有这些应用程序和测试代码都是用 Groovy (2.5.10) 编写的,但这不应该特别相关(即同样的现象应该发生在用 Java 编写的等效项目中)。

我有一个从App.start() 调用的方法,其中App 扩展javafx.application.Application(因此它在JAT/JavaFX 应用程序线程中运行):

class GraphBuilder {
...
def attachAndShow(Stage stage, Node rootNode) {
    try {
        App.instance.fxmlController.treeTableView.root = rootTreeItem
        App.instance.fxmlController.treeTableView.showRoot = false
        stage.scene = new Scene(rootNode, 1200, 800)
        stage.show()
    }catch( Throwable t ){
        log.error( "got throwable in attachAndShow... $t.message", t )
    }
}

我在“尖峰”模型(未经测试的代码,我刚刚跑在前面)的模型上进行 TDD,其中TreeTableView 被填充。我强调没有任何迹象表明尖峰有任何问题。

这样做的本质是设置TreeTableView 的列“单元格值工厂”,它规定了将被设置为TreeItems 的值的类实例的字段名称。

值类称为Task...,它的第一个字段是Stringtitle。我为此添加了第一家工厂。但是(因为我是通过 TDD 进行的)我已经为第二列添加了工厂,属性dueDate,但我还没有在Task 中包含这个字段。自然地,确保已设置具有正确属性名称的工厂的测试失败了。但我更感兴趣的是,此时我执行 Gradle installDist(即分发一个独立的可执行文件)时会发生什么。

运行:显示窗口。我可以看到TreeItems 被赋予了正确的标题。在终端中,我看到一个堆栈跟踪,原因是我未能在 Task 实例中包含必填字段 (dueDate):

Mar 23, 2020 6:16:25 PM javafx.scene.control.cell.TreeItemPropertyValueFactory getCellDataReflectively
WARNING: Can not retrieve property 'dueDate' in TreeItemPropertyValueFactory: javafx.scene.control.cell.TreeItemPropertyValueFactory@2d1e69d0 with provided class type: class core.Task
java.lang.IllegalStateException: Cannot get property dueDate
    at com.sun.javafx.property.PropertyReference.getProperty(PropertyReference.java:194)
    at javafx.scene.control.cell.TreeItemPropertyValueFactory.getCellDataReflectively(TreeItemPropertyValueFactory.java:185)
    at javafx.scene.control.cell.TreeItemPropertyValueFactory.call(TreeItemPropertyValueFactory.java:158)
    at javafx.scene.control.cell.TreeItemPropertyValueFactory.call(TreeItemPropertyValueFactory.java:136)
    at javafx.scene.control.TreeTableColumn.getCellObservableValue(TreeTableColumn.java:576)
    at javafx.scene.control.TreeTableColumn.getCellObservableValue(TreeTableColumn.java:561)
    at javafx.scene.control.TreeTableCell.updateItem(TreeTableCell.java:632)
    at javafx.scene.control.TreeTableCell.indexChanged(TreeTableCell.java:457)
    at javafx.scene.control.IndexedCell.updateIndex(IndexedCell.java:120)
    at javafx.scene.control.skin.TableRowSkinBase.updateCells(TableRowSkinBase.java:539)
    at javafx.scene.control.skin.TreeTableRowSkin.updateCells(TreeTableRowSkin.java:276)
    at javafx.scene.control.skin.TableRowSkinBase.<init>(TableRowSkinBase.java:159)
    at javafx.scene.control.skin.TreeTableRowSkin.<init>(TreeTableRowSkin.java:102)
    at javafx.scene.control.TreeTableRow.createDefaultSkin(TreeTableRow.java:529)
    at javafx.scene.control.Control.doProcessCSS(Control.java:897)
    at javafx.scene.control.Control.access$000(Control.java:83)
    at javafx.scene.control.Control$1.doProcessCSS(Control.java:89)
    at com.sun.javafx.scene.control.ControlHelper.processCSSImpl(ControlHelper.java:67)
    at com.sun.javafx.scene.NodeHelper.processCSS(NodeHelper.java:145)
    at javafx.scene.Node.processCSS(Node.java:9529)
    at javafx.scene.Node.applyCss(Node.java:9616)
    at javafx.scene.control.skin.VirtualFlow.setCellIndex(VirtualFlow.java:1715)
    at javafx.scene.control.skin.VirtualFlow.getCell(VirtualFlow.java:1692)
    at javafx.scene.control.skin.VirtualFlow.getCellLength(VirtualFlow.java:1801)
    at javafx.scene.control.skin.VirtualFlow.computeViewportOffset(VirtualFlow.java:2639)
    at javafx.scene.control.skin.VirtualFlow.layoutChildren(VirtualFlow.java:1245)
    at javafx.scene.Parent.layout(Parent.java:1204)
    at javafx.scene.Parent.layout(Parent.java:1211)
    at javafx.scene.Parent.layout(Parent.java:1211)
    at javafx.scene.Parent.layout(Parent.java:1211)
    at javafx.scene.Parent.layout(Parent.java:1211)
    at javafx.scene.Parent.layout(Parent.java:1211)
    at javafx.scene.Parent.layout(Parent.java:1211)
    at javafx.scene.Parent.layout(Parent.java:1211)
    at javafx.scene.Parent.layout(Parent.java:1211)
    at javafx.scene.Parent.layout(Parent.java:1211)
    at javafx.scene.Scene.doLayoutPass(Scene.java:576)
    at javafx.scene.Scene.preferredSize(Scene.java:1748)
    at javafx.scene.Scene$2.preferredSize(Scene.java:393)
    at com.sun.javafx.scene.SceneHelper.preferredSize(SceneHelper.java:66)
    at javafx.stage.Window$12.invalidated(Window.java:1086)
    at javafx.beans.property.BooleanPropertyBase.markInvalid(BooleanPropertyBase.java:110)
    at javafx.beans.property.BooleanPropertyBase.set(BooleanPropertyBase.java:145)
    at javafx.stage.Window.setShowing(Window.java:1174)
    at javafx.stage.Window.show(Window.java:1189)
    at javafx.stage.Stage.show(Stage.java:273)
    at javafx.stage.Stage$show.call(Unknown Source)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:47)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:115)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:119)
    at core.GraphBuilder.attachAndShow(main.groovy:111)
    at core.GraphBuilder$attachAndShow$4.call(Unknown Source)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:47)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:115)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:135)
    at core.App.start(main.groovy:56)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:846)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:455)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
    at java.base/java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
    at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
    at com.sun.glass.ui.gtk.GtkApplication.lambda$runLoop$11(GtkApplication.java:277)
    at java.base/java.lang.Thread.run(Thread.java:834)

此Throwable(IllegalState 是RuntimeException)不会被上述try ... catch 捕获。为什么不?有什么办法可以捕捉到这样的异常吗?

这显然是 JAT:您可以在该堆栈跟踪中看到 App.start()(还有 GraphBuilder.attachAndShow())

我刚刚进行了几个实验:在这个 attachAndShow() 方法中首先抛出一个 RuntimeException,然后是一个标准的 Exception,在方法中的 4 行中的任何一行之前。 p>

在这两种情况下,(分布式)应用程序都无法运行,并且什么也没有显示……但是日志消息确实被记录了,即这些都被捕获了。完全符合预期的行为,因此 JAT 本身似乎不是“罪魁祸首”。

我还注意到上面显示的跟踪不是“正常”的堆栈跟踪:前两行表明有东西正在处理它,而且很不幸地吞下了它。至少我希望能够记录此类问题!

【问题讨论】:

  • 这并不能回答问题,但真的没有理由再使用PropertyValueFactory 实现了。只需使用 lambda(或 Groovy 中合适的等效项)实现回调,然后无论如何都会在编译时捕获这些错误。我认为PropertyValueFactory 实现被设计为在反射失败时“静默”失败,因此实际上并没有抛出异常,而是发出警告。
  • 好的,谢谢你的提示。我应该说清楚,这是我使用 JavaFX 的第一个步履蹒跚的步骤,所以我对这些事情一无所知。不过,这可能不是一次性的异常情况:在show() 的管理下,可能还会出现其他可能导致类似现象的事情?
  • 不,不是。如果抛出异常(实际上是抛出,而不是在这种情况下由库类捕获、处理和报告),那么它将以正常方式传播(所以你会捕获它)。这只是因为您使用的特定类 (TreeItemPropertyValueFactory) 依赖于反射,如果提供的属性名称与模型对象中存在的实际属性名称不匹配,则会选择发出警告。
  • 对。所以我只是很不幸地选择了一件会让JavaFX新手完全困惑的东西。现在将搜索此回调解决方案(如您所知,NB Java lambdas 尚未在 Groovy(至少不是 2)中工作,但其他工具可用,是的)。您可能会推荐任何链接来显示此操作方法?哈...不需要:stackoverflow.com/a/38050982/595305....再次感谢!

标签: java multithreading exception javafx groovy


【解决方案1】:

基本答案是here。

不过,Groovy 用户可以沾沾自喜地切入一大片尖括号:我的解决方案变成了:

fxmlController.treeTableView.columns.get( 0 ).cellValueFactory =
new Callback() {
    @Override
    Binding call(Object cellDataFeatures ) {
        // NB it turns out that the first "value" is a TreeItem
        Task task  = cellDataFeatures.value.value
        Bindings.createStringBinding({ task.title })
    }
}

或者贴一些最小的可能:

fxmlController.treeTableView.columns.get( 0 ).cellValueFactory =
new Callback<TreeTableColumn.CellDataFeatures, ObservableValue>() {
    @Override
    Binding call(TreeTableColumn.CellDataFeatures cellDataFeatures ) {
        Task task  = cellDataFeatures.value.value
        Bindings.createStringBinding({ task.title })
    }
}

对于 Groovy 的“Java lambda”替代品,我们使用闭包也就不足为奇了:

fxmlController.treeTableView.columns.get( 0 ).cellValueFactory 
    = { cellDataFeatures -> cellDataFeatures.value.titleProperty() }

注意,这涉及到对 JavaFX Property(参见 here)的一点了解以及对显示实例的类的一点裁剪。

【讨论】:

    猜你喜欢
    • 2013-02-26
    • 2013-12-14
    • 1970-01-01
    • 1970-01-01
    • 2011-07-19
    • 2015-10-14
    • 1970-01-01
    • 2011-10-04
    相关资源
    最近更新 更多