java - JavaFX应用程序中的应用程序启动方法出现异常

标签 java spring exception javafx dependency-injection

乍一看,这个问题似乎是重复的。我在谷歌上做了一些搜索,但不幸的是没有一个结果与我相符。我在下面给出了问题链接。

Exception in Application start method java.lang.reflect.InvocationTargetException JavaFX image transition

JavaFX - Exception in Application start method?

Exception in Application start method

Exception in Application start method

Exception in Application start method java.lang.reflect.InvocationTargetException

好吧,让我们来谈谈我的问题。我使用 Netbeans 和 Spring 进行依赖注入(inject)。我的代码运行良好。我刚刚添加了一个新方法并将其与一个按钮连接起来。按下运行按钮后,我收到错误消息。

堆栈跟踪:

Executing C:\Users\Dell-3460\Documents\NetBeansProjects\JavaFXApplication_Vocubulary_Own\dist\run151098339\JavaFXApplication_Vocubulary_Own.jar using platform C:\Program Files\Java\jdk1.8.0_102\jre/bin/java
Start1...........
Nov 23, 2017 10:30:52 PM javafx.fxml.FXMLLoader$ValueElement processValue
WARNING: Loading FXML document with JavaFX API of version 8.0.141 by JavaFX runtime of version 8.0.102
Nov 23, 2017 10:30:53 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@1d4f4e8e: startup date [Thu Nov 23 22:30:53 BDT 2017]; root of context hierarchy
Nov 23, 2017 10:30:53 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [vocubularyBean.xml]
Exception in Application start method

从这个 StackTrace 我无法跟踪错误。然后我在 start 方法中使用了 Try-Catch 但没有确切的错误消息。

启动方法:

@Override
    public void start(Stage stage) throws Exception
        {
        System.out.println("Start1...........");
        try
          {
            Parent root = FXMLLoader.load(getClass().getResource("FXML_Main.fxml"));
            System.out.println("Start2...........");
            Scene scene = new Scene(root);
            stage.setScene(scene);
            stage.show();
          } 
        catch (Exception e)
            {
              e.printStackTrace();
            }
        }

在 Start 方法中添加 Try-Catch 后的堆栈跟踪:

Executing C:\Users\Dell-3460\Documents\NetBeansProjects\JavaFXApplication_Vocubulary_Own\dist\run151098339\JavaFXApplication_Vocubulary_Own.jar using platform C:\Program Files\Java\jdk1.8.0_102\jre/bin/java
Start1...........
Nov 23, 2017 10:30:52 PM javafx.fxml.FXMLLoader$ValueElement processValue
WARNING: Loading FXML document with JavaFX API of version 8.0.141 by JavaFX runtime of version 8.0.102
Nov 23, 2017 10:30:53 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@1d4f4e8e: startup date [Thu Nov 23 22:30:53 BDT 2017]; root of context hierarchy
Nov 23, 2017 10:30:53 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [vocubularyBean.xml]
Exception in Application start method

从这个堆栈跟踪中,我认为 FXML 文件中可能存在一些问题,因为第二行尚未打印。但我的问题是为什么它没有显示任何错误消息?我正在使用场景构建器。 我创建了一个空的 FXML 文件并且正在加载。

Controller 代码片段:

@FXML
    private void other_save_new_and_save_edit_other_word_button_action(ActionEvent actionEvent)
        {  
            bean_controller_Other_I.other_save_new_and_save_edit_other_word_button_action(actionEvent);
        }

FXML 代码片段:

 <Button layoutX="197.0" layoutY="32.0" mnemonicParsing="false" onAction="#other_save_new_and_save_edit_other_word_button_action" prefWidth="75.0" text="Save" AnchorPane.leftAnchor="197.0" />

发现问题:

public class ApplicationContextSingleTon
    {
        private static ApplicationContext applicationContext ;
        public static ApplicationContext getBean()
            {
                System.out.println(" AC_Line 1 : ");
                if(applicationContext == null)
                  {
                    System.out.println(" AC_Line 2 : ");
                    applicationContext = new ClassPathXmlApplicationContext("vocubularyBean.xml");
                    System.out.println(" AC_Line 3 : ");
                  }
                return applicationContext;
            }
    }



public interface ApplicationContext_I
    {
        ApplicationContext applicationContext = ApplicationContextSingleTon.getBean();
    }

public class Controller_Main implements Initializable,ApplicationContext_I
    {
    Controller_e2b_I bean_controller_e2b_I;
    Controller_e2e_I bean_controller_e2e_I;
    Controller_Other_I bean_controller_Other_I;

    @Override
    public void initialize(URL url, ResourceBundle rb)
        {
            bean_Initializer();
            System.out.println(".......Start........");
        }
    public void bean_Initializer()
        {
            bean_controller_e2b_I = (Controller_e2b_I) applicationContext.getBean("bean_Controller_e2b_Impl");
            bean_controller_e2b_I.setController_Main(this);

            bean_controller_e2e_I = (Controller_e2e_I) applicationContext.getBean("bean_Controller_e2e_Impl");
            bean_controller_e2e_I.setController_Main(this);

            bean_controller_Other_I = (Controller_Other_I) applicationContext.getBean("bean_Controller_Other_Impl");
            bean_controller_Other_I.setController_Main(this);
        }
//other codes
    }

<?xml version="1.0" encoding="UTF-8"?>
<beans  xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"
        >

    <bean id="bean_DAO_e2b_Impl" class="com.vocubulary.e2b.DAO_e2b_Impl" ></bean>
    <bean id="bean_Controller_e2b_Impl" class="com.vocubulary.e2b.Controller_e2b_Impl" >
        <property name="bean_DAO_e2b_I" ref="bean_DAO_e2b_Impl"/>
    </bean>

    <bean id="bean_DAO_e2e_Impl" class="com.vocubulary.e2e.DAO_e2e_Impl" ></bean>
    <bean id="bean_Controller_e2e_Impl" class="com.vocubulary.e2e.Controller_e2e_Impl" >
        <property name="bean_DAO_e2e_I" ref="bean_DAO_e2e_Impl"/>
    </bean>

    <!--start : problem Area-->
    <bean id="bean_DAO_Other_Impl" class="com.vocubulary.other.DAO_Other_Impl" ></bean>
    <bean id="bean_Controller_other_Impl" class="com.vocubulary.other.Controller_Other_Impl" >
        <property name="bean_DAO_Other_I" ref="bean_DAO_Other_Impl"/>
    </bean>
    <!--end : problem Area-->
</beans>

我认为没有必要描述代码,因为它们是不言自明的。我已将 xml 映射文件中的一些 xml 代码确定为问题。我想我终于找到了问题所在。

如果我在 xml 映射文件中的问题区域中保持代码处于 Activity 状态,那么问题就会发生。但如果我评论这些行,则该程序启动良好。

另一个问题是,如果这些行处于 Activity 状态,则在 ApplicationContextSingleTon 类中,不会打印“AC_Line 3”。表示应用程序在“AC_Line 2”处暂停。

我认为我已经正确映射了类。当我使用 Netbeans 时,如果我对映射到问题区域的类名执行 ctrl+clik,Netbeans 就会带我该类。

现在我有两个问题:

  1. 为什么我没有获得完整的堆栈跟踪?

  2. 这些行有什么问题吗?

最佳答案

我最近遇到了打印“启动方法中的异常”但没有打印堆栈跟踪的问题。捕获异常的解决方案(至少对我来说)是捕获 Throwable 而不是 Exception。示例:

@Override
public void start(Stage stage) throws Exception {
    try {
        //... start method code here
    } catch(Throwable t) {
        t.printStackTrace()
    }
}

关于java - JavaFX应用程序中的应用程序启动方法出现异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47460415/

相关文章:

c# - 无效泛型类型参数的最佳异常(exception)

javascript - 捕获由原型(prototype) javascript 框架抛出的异常

java - 尽管没有访问任何越界索引,但出现异常 "ArrayOutOfBounds:1"

java - 将 HashMap 与对象进行比较

Java headless (headless)双三次图像调整大小

java - Spring 实践 : 3rd edition: Chapter 1. ..Maven 和 POM 文件

java - JBoss 7.1 - 声明数据源并通过 JNDI 访问

java - Gradle 构建失败并出现 Javadoc NullPointerException

java - 如何在我们的 java jar 中插入用户可以访问的版本号?

spring - 在 Spring Webflux 中转换 Mono<Response<T>> 中的 Flux<T>