使用JavaFX时的InvocationTargetException

时间:2017-11-14 14:14:00

标签: java eclipse javafx gluon

我第一次尝试使用Gluon / JavaFX,但是我在运行时收到了InvocationTargetException。我在这个网站和其他网站上搜索了答案,但在尝试了每个建议后都收到了同样的问题。

主:

package Scenes;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;

public class Main extends Application{


    public void start(Stage stage) throws Exception{

        Parent root = FXMLLoader.load(getClass().getResource("test.fxml"));
        Scene scene = new Scene(root);
        stage.setScene(scene);
        stage.setTitle("asdf");
        stage.show();
    }

    public static void main(String[] args) {

        launch(args);
    }



}

控制器:

package Scenes;
import java.io.File;

import javax.swing.JButton;
import javax.swing.JFileChooser;

import javafx.scene.control.Button;


public class Controller {

    Button kButton;
    Button bpButton;

    public void initialize() {

    }

    public static void browseFileK() {
        JFileChooser jfc = new JFileChooser();
        int returnValue = jfc.showOpenDialog(null);
        if(returnValue == JFileChooser.APPROVE_OPTION)
        {
            File selectedFile = jfc.getSelectedFile();
        }
    }

        public static void browseFileBP() {
            JFileChooser jfc = new JFileChooser();
            int returnValue = jfc.showOpenDialog(null);
            if(returnValue == JFileChooser.APPROVE_OPTION)
            {
                File selectedFile = jfc.getSelectedFile();
            }
        }

}

FXML文件:

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

<?import com.gluonhq.charm.glisten.control.TextField?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>

<VBox prefHeight="400.0" prefWidth="640.0" xmlns="http://javafx.com/javafx/8.0.151" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
  <children>
    <AnchorPane cache="true" maxHeight="-1.0" maxWidth="-1.0" prefHeight="-1.0" prefWidth="-1.0" VBox.vgrow="ALWAYS">
      <children>
        <Label alignment="CENTER" layoutX="155.0" layoutY="177.0" style="&#10;" textAlignment="CENTER" textFill="#9f9f9f" wrapText="false">
          <font>
            <Font size="18.0" />
          </font>
        </Label>
            <TextField layoutX="21.0" layoutY="47.0" prefHeight="26.0" prefWidth="237.0" text="Select location of Kasasa accounts file" />
            <TextField layoutX="21.0" layoutY="118.0" prefHeight="26.0" prefWidth="237.0" text="Select location of Bill Pay accounts file" />
            <Button layoutX="294.0" layoutY="48.0" mnemonicParsing="false" text="Browse..." />
            <Button layoutX="294.0" layoutY="115.0" mnemonicParsing="false" text="Browse..." />
            <Button layoutX="168.0" layoutY="269.0" mnemonicParsing="false" prefHeight="32.0" prefWidth="253.0" text="Generate refund document" />
      </children>
    </AnchorPane>
  </children>
</VBox>

堆栈跟踪:

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: java.lang.NullPointerException: Location is required.
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3207)
    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 Scenes.Main.start(Main.java:14)
    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

似乎最流行的建议是将fxml文件添加到与main相同的包中,添加&#34; /&#34;在fxml文件前面,或者将文件名更改为&#34; Scenes / test.fxml&#34;,其中没有一个对我有效。

2 个答案:

答案 0 :(得分:2)

您的问题的答案在很大程度上取决于您的构建系统正在做什么。例如,某些构建系统不会将.fxml文件复制到构建目录中,如果它不在某个resources文件夹而不是源文件夹中。其他人需要明确指定要复制到构建文件夹的文件模式。

调试此方法的最快方法是检查构建文件夹(输出目录)并查看.fxml文件是否存在。

答案 1 :(得分:0)

您的FXML文件说:

fx:controller="sample.Controller"

但您的Controller位于

 package Scenes;

所以你的FXML应该是

fx:controller="Scenes.Controller"

您正在使用Swing个组件。

import javax.swing.JButton;
import javax.swing.JFileChooser;

您需要使用JavaFx个组件。

import javafx.scene.control.Button;
import javafx.stage.FileChooser;