简体   繁体   English

JavaFX:应用程序启动方法中的异常 java.lang.reflect.InvocationTargetException

[英]JavaFX: Exception in Application Start Method java.lang.reflect.InvocationTargetException

I am literally desperate.我真的很绝望。 I don't know why but every time I try to run my program this error comes out.我不知道为什么,但每次我尝试运行我的程序时都会出现这个错误。 I am using Netbeans, with Java 10.0.2 in order to have already installed JavaFX.我正在使用 Netbeans 和 Java 10.0.2,以便已经安装了 JavaFX。 I think it's due to some problem with the FXML file.我认为这是由于 FXML 文件存在问题。 Can you help me?你能帮助我吗?

java.lang.reflect.InvocationTargetException
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:564)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:473)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:372)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:564)
    at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:941)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:973)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:198)
    at java.base/java.lang.Thread.run(Thread.java:844)
Caused by: javafx.fxml.LoadException: Root hasn't been set. Use method setRoot() before load.
file:/C:/Users/User/Documents/NetBeansProjects/gruppo71/dist/run2124011701/gruppo71.jar!/gruppo71/FXMLDocument.fxml:13

    at javafx.fxml/javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2621)
    at javafx.fxml/javafx.fxml.FXMLLoader.access$100(FXMLLoader.java:105)
    at javafx.fxml/javafx.fxml.FXMLLoader$RootElement.constructValue(FXMLLoader.java:1338)
    at javafx.fxml/javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:754)
    at javafx.fxml/javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2722)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2552)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2466)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3253)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3210)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3179)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3152)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3129)
    at javafx.fxml/javafx.fxml.FXMLLoader.load(FXMLLoader.java:3122)
    at gruppo71.Gruppo71.start(Gruppo71.java:21)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:919)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$11(PlatformImpl.java:449)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$9(PlatformImpl.java:418)
    at java.base/java.security.AccessController.doPrivileged(Native Method)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:417)
    at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:175)
    ... 1 more
Exception running application gruppo71.Gruppo71
Java Result: 1

My main is this:我的主要是这样的:


import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

/**
 *
 * @author User
 */
public class Gruppo71 extends Application {
    
    @Override
    public void start(Stage stage) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
        
        Scene scene = new Scene(root);
        
        stage.setScene(scene);
        stage.show();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }
} 

My FXML code is:我的 FXML 代码是:


<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ContextMenu?>
<?import javafx.scene.control.DatePicker?>
<?import javafx.scene.control.MenuItem?>
<?import javafx.scene.control.SplitPane?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>

<fx:root id="AnchorPane" prefHeight="300.0" prefWidth="450.0" type="AnchorPane" xmlns="http://javafx.com/javafx/10.0.2" xmlns:fx="http://javafx.com/fxml/1" fx:controller="gruppo71.FXMLDocumentController">
   <children>
      <SplitPane dividerPositions="0.13087248322147652" layoutX="28.0" orientation="VERTICAL" prefHeight="200.0" prefWidth="320.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
        <items>
          <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="100.0" prefWidth="160.0">
               <children>
                  <Button fx:id="aggiungiButton" layoutX="10.0" layoutY="5.0" mnemonicParsing="false" onAction="#addEvento" text="Aggiungi Evento" AnchorPane.bottomAnchor="5.0" AnchorPane.leftAnchor="10.0" AnchorPane.topAnchor="5.0" />
                  <DatePicker fx:id="dataPicker" layoutX="114.0" layoutY="6.0" prefHeight="26.0" prefWidth="150.0" AnchorPane.bottomAnchor="5.0" AnchorPane.leftAnchor="124.0" AnchorPane.topAnchor="5.0" />
                  <TextField fx:id="descriptionField" layoutX="285.0" layoutY="6.0" text="aggiungi descrizione..." AnchorPane.bottomAnchor="5.0" AnchorPane.leftAnchor="284.0" AnchorPane.rightAnchor="10.0" AnchorPane.topAnchor="5.0" />
               </children>
            </AnchorPane>
          <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="100.0" prefWidth="160.0">
               <children>
                  <TableView fx:id="mainTab" layoutX="14.0" prefHeight="256.0" prefWidth="448.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
                    <columns>
                      <TableColumn fx:id="dataColumn" prefWidth="136.0" text="Data" />
                      <TableColumn fx:id="eventColumn" prefWidth="311.0" text="Evento" />
                    </columns>
                     <contextMenu>
                        <ContextMenu>
                           <items>
                              <MenuItem fx:id="removeButton" mnemonicParsing="false" onAction="#removeEvent" text="Rimuovi Evento" />
                              <MenuItem fx:id="importaButton" mnemonicParsing="false" onAction="#importCSV" text="Importa CSV" />
                              <MenuItem fx:id="exportButton" mnemonicParsing="false" onAction="#exportCSV" text="Esporta CSV" />
                           </items>
                        </ContextMenu>
                     </contextMenu>
                  </TableView>
               </children>
            </AnchorPane>
        </items>
      </SplitPane>
   </children>
</fx:root>

All help would be greatly appreciated!!!所有帮助将不胜感激!!!

Just to mark this as answered: See @James_D' comment for the solution.只是将其标记为已回答:有关解决方案,请参见 @James_D' 评论。

Replace <fx:root> in the FXML file with <AnchorPane> or set the root on the FXMLLoader before calling load() for example like this:将 FXML 文件中的<fx:root>替换为<AnchorPane>或在调用load()之前在FXMLLoader上设置根,例如:

FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("FXMLDocument.fxml"));
fxmlLoader.setRoot(new AnchorPane());
Parent root = fxmlLoader.load();

See also: JavaFx FXML load file issues with setting root另请参阅: JavaFx FXML 加载文件问题与设置 root

暂无
暂无

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

相关问题 应用程序启动方法 java.lang.reflect.InvocationTargetException 中的异常 - Exception in Application start method java.lang.reflect.InvocationTargetException 应用程序启动方法java.lang.reflect.InvocationTargetException中的异常[NO FXML] - Exception in Application start method java.lang.reflect.InvocationTargetException [NO FXML] 应用程序启动方法 java.lang.reflect.InvocationTargetException 中的 JavaFX 异常问题 - Problem with JavaFX Exception in Application start method java.lang.reflect.InvocationTargetException 应用程序启动方法java.lang.reflect.InvocationTargetException Javafx fxml中的异常 - Exception in Application start method java.lang.reflect.InvocationTargetException Javafx fxml 应用程序启动方法java.lang.reflect.InvocationTargetException中的异常JavaFX图像过渡 - Exception in Application start method java.lang.reflect.InvocationTargetException JavaFX image transition 应用程序构造函数java.lang.reflect.InvocationTargetException中的JavaFX异常 - JavaFX Exception in Application constructor java.lang.reflect.InvocationTargetException javafx中的java.lang.reflect.InvocationTargetException中的异常 - Exception in java.lang.reflect.InvocationTargetException in javafx 应用程序启动方法 java.lang.reflect.InvocationTargetException 在 sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 中的异常 - Exception in Application start method java.lang.reflect.InvocationTargetException at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) JavaFX:线程“ JavaFX Application Thread”中的异常java.lang.RuntimeException:java.lang.reflect.InvocationTargetException - JavaFX: Exception in thread “JavaFX Application Thread” java.lang.RuntimeException: java.lang.reflect.InvocationTargetException JavaFX:线程“ JavaFX Application Thread”中的异常java.lang.RuntimeException:java.lang.reflect.InvocationTargetException - JavaFX: Exception in thread “JavaFX Application Thread” java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM