javafx的Application start方法中的异常

时间:2017-05-26 19:18:02

标签: java eclipse javafx

我将Java程序与JavaFX连接起来有问题。

当我点击带有main的类→以java应用程序运行时,会出现以下问题: 这个问题简要说明如下:

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$155(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 application.Main.start(Main.java:31)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(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$148(WinApplication.java:191)
    ... 1 more
Exception running application application.Main

我的代码只是在布局中添加按钮。 我的代码如下所示:

package application;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javafx.application.Application;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import java.lang.*;

public class Main extends Application{
    public static void main(String[] args){
        launch(args);
    }

    public void start(Stage arg0) throws Exception {
        Parent root= FXMLLoader.load(getClass().getResource("shata.fxml"));
        Scene scene=new Scene(root);
        arg0.setScene(scene);
        arg0.show();
    }
}

我的FXML是:

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

<?import com.gluonhq.charm.glisten.control.BottomNavigation?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ButtonBar?>
<?import javafx.scene.layout.AnchorPane?>


<AnchorPane prefHeight="179.0" prefWidth="316.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.111">
   <children>
      <BottomNavigation layoutX="46.0" layoutY="51.0">
          <fx:define>
              <ToggleGroup fx:id="toggleGroup" />
          </fx:define>
      </BottomNavigation>
      <ButtonBar layoutX="103.0" layoutY="63.0" prefHeight="40.0" prefWidth="66.0">
        <buttons>
          <Button minWidth="40.0" mnemonicParsing="false" prefHeight="25.0" text="Button" />
        </buttons>
      </ButtonBar>
   </children>
</AnchorPane>

2 个答案:

答案 0 :(得分:0)

你把shata.fxml文件放在哪里?应用程序希望与Main.class位于同一目录中。

答案 1 :(得分:0)

我尝试运行你的代码,但只把Button放在AnchorPane中,没有别的。

它对我来说很好。您是否尝试过在没有自定义ButtonNavigation的情况下运行它?也许这可能会导致问题?

我经常发现当Java给FX提供关于FXML文件的异常时,它是由不同的东西引起的。