2017-05-26 56 views
0

我想要將Java程序與JavaFX連接時出現問題。javafx的應用程序啓動方法中的例外

當我點擊與主→運行的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> 

回答

0

你在哪裏把你的shata.fxml文件?應用程序期望它與Main.class位於同一目錄中。

0

我試着運行你的代碼,但只把Button放在AnchorPane中,沒有別的。

它適用於我。你有沒有嘗試運行它沒有自定義ButtonNavigation?也許這可能會導致問題?

我經常發現,當Java給出一個關於FXML文件的異常時,它是由不同的東西引起的。

相關問題