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

时间:2019-12-11 17:35:39

标签: javafx

我正在尝试将fxml文档绑定到控制器类,但不确定如何工作。我正在使用下载到eclipse上的javafx,并且正在使用外部场景构建器对其进行编辑。

这是我得到的错误

    Exception in Application start method
java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
    at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at sun.launcher.LauncherHelper$FXHelper.main(Unknown Source)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$154(LauncherImpl.java:182)
    at java.lang.Thread.run(Unknown Source)
Caused by: javafx.fxml.LoadException: 
/C:/Users/david/eclipse-workspace/CompSciIA/bin/application/MainFXML.fxml

    at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2543)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
    at application.Main.start(Main.java:26)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$161(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$174(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177)
    ... 1 more
Caused by: javax.xml.stream.XMLStreamException: ParseError at [row,col]:[8,5]
Message: Content is not allowed in prolog.
    at com.sun.org.apache.xerces.internal.impl.XMLStreamReaderImpl.next(Unknown Source)
    at javax.xml.stream.util.StreamReaderDelegate.next(Unknown Source)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2513)
    ... 17 more
Exception running application application.Main

这是我的fxml文档

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Menu?>
<?import javafx.scene.control.MenuBar?>
<?import javafx.scene.control.MenuItem?>
<?import javafx.scene.layout.AnchorPane?>
       fx:controller="application.ControllerMain">


<AnchorPane fx:id="Pane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1">
       fx:controller="application.ControllerMain">
   <children>
      <MenuBar fx:id="MenuBar" prefHeight="25.0" prefWidth="147.0">
        <menus>
          <Menu mnemonicParsing="false" text="File">
            <items>
              <MenuItem mnemonicParsing="false" text="Close" />
            </items>
          </Menu>
          <Menu mnemonicParsing="false" text="Edit">
            <items>
              <MenuItem mnemonicParsing="false" text="Delete" />
            </items>
          </Menu>
          <Menu mnemonicParsing="false" text="Help">
            <items>
              <MenuItem mnemonicParsing="false" text="About" />
            </items>
          </Menu>
        </menus>
      </MenuBar>
      <Button fx:id="NextButton" layoutX="518.0" layoutY="343.0" mnemonicParsing="false" text="Next" />
   </children>
</AnchorPane>

这是我的控制器类

package application;

public class ControllerMain {

}

这是我的主班

package application;

import java.io.FileInputStream;
import java.io.IOException;

//importing necessary packages
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;


public class Main extends Application {

    Stage window;
    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws IOException {

        Parent scene = FXMLLoader.load(getClass().getResource("/application/MainFXML.fxml"));
        primaryStage.setTitle("Behavior Trac");
        primaryStage.setScene(new Scene(scene,600,475));
        primaryStage.show();

        //FXMLLoader loader = new FXMLLoader();
        // Path to the FXML File
        //String fxmlDocPath = "C:\\Users\\david\\eclipse-workspace\\CompSciIA\\src/MainFXML.fxml";
//      FileInputStream fxmlStream = new FileInputStream(fxmlDocPath);
//       // Create the Pane and all Details
//        VBox root = (VBox) loader.load(fxmlStream);
// 
//        // Create the Scene
//        Scene scene = new Scene(root);
//        
//      // Set the Scene to the Stage
//        window.setScene(scene);
//        // Set the Title to the Stage
//        window.setTitle("A FXML Example with a Script Event Handler");
//        // Display the Stage
//        window.show();

    }//end public void


}//end main

当我从fxml文档中删除这一行但我想将其绑定到ControllerMain类时,我的代码有效

       fx:controller="application.ControllerMain">

0 个答案:

没有答案
相关问题