javaFX警告抛出无法捕获的非法状态异常?

时间:2015-06-26 14:14:55

标签: java exception javafx

在包含JavaFX警报的类上运行一些单元测试,我实现了以下try-catch块:

    try{
    if(warning){
        //CONSIDER:  Make a generic alert call for any situation, pass args 
        Alert alert = new Alert(AlertType.WARNING);
        alert.setTitle("User Warning");
        alert.setHeaderText(null);
        alert.setContentText("The following elements were not found for the code you are outputting and are printed, as is"
                + "\n\n" + errorList
                + "\n\n(This should be updated to Z000 format) ");
        alert.showAndWait();
    }}
    //throws error when invoked from a non javaFX context
    catch(IllegalStateException e){        
        System.out.println("The following elements were not found for the code you are outputting and are printed, as is"
                + "/n/n" + errorList
                + "/n/n(This should be updated to Z000 format)");
    }

在没有修复它的调用代码上进行顶级try-catch之后。

Stack:

Exception in thread "main" java.lang.ExceptionInInitializerError
    at javafx.scene.control.DialogPane.createContentLabel(DialogPane.java:166)
    at javafx.scene.control.DialogPane.<init>(DialogPane.java:217)
    at javafx.scene.control.Dialog.<init>(Dialog.java:478)
    at javafx.scene.control.Alert.<init>(Alert.java:245)
    at javafx.scene.control.Alert.<init>(Alert.java:223)
    at gov.ornl.nstd.datatools.OutputFormatter.verifyAndNorm(OutputFormatter.java:385)
    at gov.ornl.nstd.datatools.OutputFormatter.convert(OutputFormatter.java:218)
    at gov.ornl.nstd.datatools.Testing.TestAllOutputs.main(TestAllOutputs.java:49)
Caused by: java.lang.IllegalStateException: Toolkit not initialized
    at com.sun.javafx.application.PlatformImpl.runLater(PlatformImpl.java:273)
    at com.sun.javafx.application.PlatformImpl.runLater(PlatformImpl.java:268)
    at com.sun.javafx.application.PlatformImpl.setPlatformUserAgentStylesheet(PlatformImpl.java:550)
    at com.sun.javafx.application.PlatformImpl.setDefaultPlatformUserAgentStylesheet(PlatformImpl.java:512)
    at javafx.scene.control.Control.<clinit>(Control.java:87)
    ... 8 more

有哪些方法可以解决这个问题?

我从错误中得到的是没有与警报关联的JavaFX场景/阶段,但我不知道如何处理这个问题。

2 个答案:

答案 0 :(得分:1)

JavaFX在启动时执行“隐藏”初始化。作为单元测试运行警报不会触发初始化。触发它的最简单方法是执行Application.launch()。还有其他方法可以查看几个链接

答案 1 :(得分:0)

解决问题就像在您正在运行测试的类中扩展Application一样简单。

这不会生成警告框,但可以避免崩溃。

编辑:这会使警报无法处理,需要强制退出该过程。