2017-07-13 7 views
0

シーンビルダを使って簡単なシーンを構築しようとしました。しかし、私はコードを実行するmain.javaファイルを作ることに失敗しました!私は何が間違っているのか理解できません!シーンビルダによって設計されたアプリケーションをmain.javaで実行するにはどうすればいいですか?

は、ここに私のFXMLDocumentController.javaファイルです:

import javafx.event.ActionEvent; 
import javafx.fxml.FXML; 

public class FXMLDocumentController { 

    @FXML 
    void btnHandle(ActionEvent event) { 
    } 

} 

そしてここFXMLファイルです:私はこれを実行すると sample.fxml

そしてMain.java

import javafx.application.Application; 
import static javafx.application.Application.launch; 
import javafx.fxml.FXMLLoader; 
import javafx.scene.Parent; 
import javafx.scene.Scene; 
import javafx.scene.layout.Pane; 
import javafx.stage.Stage; 
import javafx.stage.StageStyle; 

public class Main extends Application { 

    @Override 
    public void start(Stage stage) throws Exception { 
     Parent root = FXMLLoader.load(getClass().getResource("sample.fxml")); 

     Scene scene = new Scene(root); 

     stage.setScene(scene); 
     stage.show(); 
    } 

    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String[] args) { 
     launch(args); 
    } 
} 

コードにエラーが表示され、ビルドできませんでした:0アプリケーションの起動方法

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$155(LauncherImpl.java:182) 
    at java.lang.Thread.run(Thread.java:745) 
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 Main.start(Main.java:15) 
    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 Main 
C:\Users\Dell\Desktop\Exercise\Java\BasicXml\nbproject\build-impl.xml:1052: The following error occurred while executing this line: 
C:\Users\Dell\Desktop\Exercise\Java\BasicXml\nbproject\build-impl.xml:806: Java returned: 1 
BUILD FAILED (total time: 0 seconds) 
+2

正確なエラーが参考になる私たちに告げます。 – csmckelvey

+0

私は、コードを実行すると、それはアプリケーションの起動方法に誤り::: 例外 にjava.lang.reflect.InvocationTargetException sun.reflect.NativeMethodAccessorImplで sun.reflect.NativeMethodAccessorImpl.invoke0(ネイティブメソッド)で\t \t示しています。呼び出し(NativeMethodAccessorImpl.java:62)sun.reflect.DelegatingMethodAccessorImpl.invokeで \t(DelegatingMethodAccessorImpl.java:43) ............. BUILD FAILED(合計時間:0秒) –

+0

_whole_ stacktraceを投稿して、コメントではなく質問で実行してください。 – csmckelvey

答えて

0

で例外は私が親からAnchorPaneメインにルートクラスをcangeすることをお勧め:

public class Main extends Application { 
    @Override 
    public void start(Stage stage) throws Exception { 

    AnchorPane root = FXMLLoader.load(getClass().getResource("sample.fxml")); 

    Scene scene = new Scene(root); 

    stage.setScene(scene); 
    stage.show(); 
} 

/** 
* @param args the command line arguments 
*/ 
    public static void main(String[] args) { 
    launch(args); 
    } 
} 
関連する問題