start方法中的JavaFX InvocationTargetException

时间:2018-02-26 01:11:50

标签: java javafx javafx-8

我正在创建一个简单的JavaFX应用程序,它正在抛出InvocationTargetException

当我尝试加载FXML文件时,抛出的异常与路径规范中的错误有关;因此,为了确保这是异常背后的原因,我尝试创建一个简单的JavaFX应用程序,在场景中显示一个按钮。无论如何抛出同样的异常。

我找到了一些有关此问题的解决方案,我尝试了所有解决方案,例如删除
来自fxml文件路径的\并创建FXMLLoader的实例,而不是依赖于静态load(...)方法。这些都不适合我。

主类:

 package UI;

 import java.io.IOException;
 import javafx.application.Application;
 import javafx.event.ActionEvent;
 import javafx.event.EventHandler;
 import javafx.fxml.FXMLLoader;
 import javafx.scene.Scene;
 import javafx.scene.control.Button;
 import javafx.scene.layout.BorderPane;
 import javafx.scene.layout.StackPane;
 import javafx.stage.Stage;


 public class MainFX extends Application {

 private Stage stage;
 private BorderPane borderPane;
 @Override
 public void start(Stage stage) throws IOException {
   this.stage = stage;
   this.stage.setTitle("App");
   showMainView();
 }
 private void showMainView() throws IOException
 {
    FXMLLoader loader = new FXMLLoader();
    loader.setLocation(MainFX.class.getResource("login.fxml"));
    borderPane = loader.load();
    Scene scene = new Scene(borderPane);
    stage.setScene(scene);
    stage.show();
 }
 public static void main(String[] args)
 {
    launch(args);
 }
}

login.fxml:

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

<?import com.jfoenix.controls.JFXButton?>
<?import com.jfoenix.controls.JFXPasswordField?>
<?import com.jfoenix.controls.JFXSpinner?>
<?import com.jfoenix.controls.JFXTextField?>
<?import javafx.scene.effect.Light.Distant?>
<?import javafx.scene.effect.Lighting?>
<?import javafx.scene.effect.Shadow?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.text.Font?>
<?import javafx.scene.text.Text?>


<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="480.0" prefWidth="640.0" style="-fx-background-color: #222629;" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1" fx:controller="LoginController">
   <center>
      <GridPane BorderPane.alignment="CENTER">
        <columnConstraints>
          <ColumnConstraints hgrow="SOMETIMES" maxWidth="208.0" minWidth="10.0" prefWidth="166.0" />
            <ColumnConstraints hgrow="SOMETIMES" maxWidth="414.0" minWidth="10.0" prefWidth="266.0" />
          <ColumnConstraints hgrow="SOMETIMES" maxWidth="168.0" minWidth="10.0" prefWidth="168.0" />
        </columnConstraints>
        <rowConstraints>
            <RowConstraints fillHeight="false" maxHeight="-Infinity" minHeight="10.0" prefHeight="50.0" vgrow="SOMETIMES" />
          <RowConstraints fillHeight="false" maxHeight="-Infinity" minHeight="5.0" prefHeight="50.0" vgrow="SOMETIMES" />
            <RowConstraints fillHeight="false" maxHeight="-Infinity" minHeight="10.0" prefHeight="50.0" vgrow="SOMETIMES" />
            <RowConstraints fillHeight="false" maxHeight="-Infinity" minHeight="10.0" prefHeight="50.0" vgrow="SOMETIMES" />
          <RowConstraints fillHeight="false" maxHeight="-Infinity" minHeight="5.0" prefHeight="50.0" vgrow="SOMETIMES" />
          <RowConstraints fillHeight="false" maxHeight="-Infinity" minHeight="5.0" prefHeight="50.0" vgrow="SOMETIMES" />
            <RowConstraints fillHeight="false" maxHeight="-Infinity" minHeight="10.0" prefHeight="50.0" vgrow="SOMETIMES" />
            <RowConstraints fillHeight="false" maxHeight="-Infinity" minHeight="10.0" prefHeight="50.0" vgrow="SOMETIMES" />
        </rowConstraints>
         <children>
            <JFXTextField focusColor="#999ea1" promptText="Email" style="-fx-text-inner-color: #999ea1;" unFocusColor="#474b4f" GridPane.columnIndex="1" GridPane.rowIndex="4" />
            <JFXPasswordField focusColor="#999ea1" labelFloat="true" promptText="Password" style="-fx-text-inner-color: #999ea1;" unFocusColor="#474b4f" GridPane.columnIndex="1" GridPane.rowIndex="5" />
            <Text fill="WHITE" lineSpacing="50.0" pickOnBounds="true" smooth="false" strokeWidth="0.0" text="Login" textAlignment="CENTER" wrappingWidth="265.443359375" GridPane.columnIndex="1" GridPane.rowIndex="1">
               <font>
                  <Font name="Arial Bold" size="34.0" />
               </font>
            </Text>
            <GridPane GridPane.columnIndex="1" GridPane.rowIndex="7">
              <columnConstraints>
                <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
              </columnConstraints>
              <rowConstraints>
                <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
              </rowConstraints>
               <children>
                  <JFXButton alignment="TOP_CENTER" prefHeight="25.0" prefWidth="135.0" ripplerFill="#979797" text="Cancel" textFill="WHITE" />
                  <JFXButton alignment="TOP_CENTER" prefHeight="25.0" prefWidth="137.0" ripplerFill="#86c232" style="-fx-background-color: #61892F;" text="Login" textFill="WHITE" GridPane.columnIndex="1" />
               </children>
            </GridPane>
            <JFXSpinner prefHeight="22.0" prefWidth="198.0" GridPane.columnIndex="1" GridPane.rowIndex="6">
               <effect>
                  <Lighting>
                     <bumpInput>
                        <Shadow />
                     </bumpInput>
                     <light>
                        <Light.Distant />
                     </light>
                  </Lighting>
               </effect>
            </JFXSpinner>
         </children>
      </GridPane>
   </center>
</BorderPane>

栈跟踪:

Exception in Application start method
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at         
sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
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(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
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(Thread.java:748)
Caused by: javafx.fxml.LoadException: 

file:/C:/Users/User/Documents/NetBeansProjects/ JavaFXApplication2/dist/run966830135/JavaFXApplication2.jar!/login.fxml

at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
at javafx.fxml.FXMLLoader.importClass(FXMLLoader.java:2848)
at javafx.fxml.FXMLLoader.processImport(FXMLLoader.java:2692)
at javafx.fxml.FXMLLoader.processProcessingInstruction(FXMLLoader.java:2661)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2517)
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 Main.start(Main.java:21)
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: java.lang.ClassNotFoundException: com.jfoenix.controls.JFXButton
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:338)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at javafx.fxml.FXMLLoader.loadTypeForPackage(FXMLLoader.java:2916)
at javafx.fxml.FXMLLoader.loadType(FXMLLoader.java:2905)
at javafx.fxml.FXMLLoader.importClass(FXMLLoader.java:2846)
... 20 more
Exception running application Main
Java Result: 1

1 个答案:

答案 0 :(得分:1)

您已将Jfoenix API导入到场景构建器中,但未将其包含在项目的构建路径中。

将JAR文件添加到项目的确切方法取决于您的IDE。你可以快速谷歌并自己找到它。