简体   繁体   English

JavaFX 应用程序中应用程序启动方法中的异常

[英]Exception in Application start method in JavaFX Application

At the first glance the question may seems to be duplicated.乍一看,这个问题似乎是重复的。 I have done some searches in Google.But unfortunately none of the results dont match with me.我在谷歌做了一些搜索。但不幸的是,没有一个结果与我不匹配。 I have given the questions link below.我已经给出了下面的问题链接。

Exception in Application start method java.lang.reflect.InvocationTargetException JavaFX image transition 应用程序启动方法中的异常 java.lang.reflect.InvocationTargetException JavaFX 图像转换

JavaFX - Exception in Application start method? JavaFX - 应用程序启动方法中的异常?

Exception in Application start method 应用程序启动方法中的异常

Exception in Application start method 应用程序启动方法中的异常

Exception in Application start method java.lang.reflect.InvocationTargetException 应用程序启动方法 java.lang.reflect.InvocationTargetException 中的异常

Ok, Let come to my problem.好的,让我们来解决我的问题。 I am using Netbeans and Spring for dependency injection.我使用 Netbeans 和 Spring 进行依赖注入。 My code was working fine.我的代码运行良好。 I just added a new method and connected it with a button.我刚刚添加了一个新方法并用一个按钮将它连接起来。 After pressing the run button i have got the error.按下运行按钮后,我收到错误消息。

Stack Trace:堆栈跟踪:

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

From this StackTrace i am unable to trace the error.从这个 StackTrace 我无法追踪错误。 Then i have used Try-Catch within start method but no exact error message.然后我在 start 方法中使用了 Try-Catch 但没有确切的错误消息。

Start Method :启动方法:

@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();
            }
        }

Stack Trace After Adding Try-Catch in Start Method :在 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

From this stack trace i am thinking there might some problems in FXML file as second line has not printed.从这个堆栈跟踪中,我认为 FXML 文件中可能存在一些问题,因为第二行没有打印出来。 But my question is why it is not showing any error message?但我的问题是为什么它没有显示任何错误消息? I am using SceneBuilder.我正在使用 SceneBuilder。 I have created an empty FXML file and it is loading.我创建了一个空的 FXML 文件,它正在加载。

Snippet of Controller Code :控制器代码片段:

@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);
        }

Snippet of FXML Code: 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" />

Problem Identified :问题确定:

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>

I think no need to describe codes as they are self explanatory.我认为无需描述代码,因为它们是不言自明的。 I have identified some xml codes as problem in the xml mapping file.我在 xml 映射文件中发现一些 xml 代码有问题。 I think finally i have identified the problem.我想我终于确定了问题所在。

If i am keeping the codes active within the problem area in xml mapping file then the problem is happening.如果我在 xml 映射文件中的问题区域内保持代码处于活动状态,则问题正在发生。 But if i am commenting those lines, the program is starting well.但是,如果我评论这些行,则该程序运行良好。

And another problem is if those lines are active, within ApplicationContextSingleTon class, "AC_Line 3" is not being printed.另一个问题是,如果这些行处于活动状态,则在 ApplicationContextSingleTon 类中,不会打印“AC_Line 3”。 Means the application is being halted at "AC_Line 2".表示应用程序在“AC_Line 2”处停止。

I think i have mapped the classes properly.我想我已经正确地映射了这些类。 As i am using Netbeans, if i do ctrl+clik on the class names, mapped in the problem area, Netbeans is taking me the class.当我使用 Netbeans 时,如果我对映射在问题区域中的类名执行 ctrl+clik,则 Netbeans 会带我去上课。

Now i have two Questions :现在我有两个问题:

  1. Why i am not getting full stack trace?为什么我没有得到完整的堆栈跟踪?

  2. What is the wrong with those lines?这些线有什么问题?

I just barely ran into this issue, and could not figure out why the exception was not printing.我刚刚遇到这个问题,无法弄清楚为什么没有打印异常。 Like you, I tried surrounding my entire Start method in a try-catch.像您一样,我尝试在 try-catch 中围绕我的整个 Start 方法。 It turns out that STDERR isn't being sent to the proper location.事实证明,STDERR 没有被发送到正确的位置。 The way I found this out was by surrounding with a try-catch (again), and using System.out.println(ex) instead of ex.printStackTrace() .我发现这一点的方法是用 try-catch(再次)包围,并使用System.out.println(ex)而不是ex.printStackTrace() You could also use ex.printStackTrace(System.out) .您也可以使用ex.printStackTrace(System.out)

I suppose I am late to the party, but hopefully I can help anybody who may encounter this same issue.我想我参加聚会迟到了,但希望我可以帮助任何可能遇到同样问题的人。

I have recently run into the issue with "Exception in start method" being printed but no stack trace being printed.我最近遇到了打印“启动方法中的异常”但没有打印堆栈跟踪的问题。 The solution (at least for me) to catching the exception is to catch a Throwable rather than an Exception .捕获异常的解决方案(至少对我而言)是捕获Throwable而不是Exception Example:例子:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM