2017-02-20 21 views
0

私はPluralSiteのこのコースに従っていますが、コードをコピーしていますが、彼はnetbeansを使用しています。私はeclipseを使用しています。何が起こっているのかわかりません。 netbeansとecpliseを使用してfxmlファイルを使用すると違いがありますか?私は誰かを助けることができる場合appriciateだろう。それ以上の情報が必要な場合は、ぜひ尋ねてみてください。私はjavafxに新しいので、いいです。 FXMLファイルは、他の人と同じパッケージにある:ここFXMLファイルでアプリケーションが開かない

アプリのコードに続いて、私が得る例外で、その後、FXMLコードを修正

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.SimpleUI.start(SimpleUI.java:18) 
    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.SimpleUI 





    //first is my app 
    package application; 

    import java.io.IOException; 

    import javafx.application.Application; 
    import javafx.fxml.FXMLLoader; 
    import javafx.scene.Scene; 
    import javafx.scene.control.Button; 
    import javafx.scene.control.Label; 
    import javafx.scene.control.TextField; 
    import javafx.scene.layout.HBox; 
    import javafx.scene.layout.VBox; 
    import javafx.stage.Stage; 

    public class SimpleUI extends Application { 
     @Override 
     public void start(Stage primaryStage) throws Exception { 
      HBox box =  FXMLLoader.load(getClass().getResource("/hey/src/application/simpleui.fxml")); 

    //  VBox vBox = new VBox(new Label("This goes down"), new Button("Down"), new Button("Further Down")); 
    // 
    //  box.getChildren().addAll(new Label("UserName"), new TextField(), new Button("Connect"), vBox); 

      Scene scene = new Scene(box); 
      primaryStage.setScene(scene); 
      primaryStage.show(); 
     } 

     public static void main(String[] args) { 
      launch(args); 
     } 
    } 



    //this is my fxml code 
    import java.io.IOException; 

    import javafx.application.Application; 
    import javafx.fxml.FXMLLoader; 
    import javafx.scene.Scene; 
    import javafx.scene.control.Button; 
    import javafx.scene.control.Label; 
    import javafx.scene.control.TextField; 
    import javafx.scene.layout.HBox; 
    import javafx.scene.layout.VBox; 
    import javafx.stage.Stage; 

    public class SimpleUI extends Application { 
     @Override 
     public void start(Stage primaryStage) throws Exception { 
      HBox box = FXMLLoader.load(getClass().getResource("/hey/src/application/simpleui.fxml")); 

    //  VBox vBox = new VBox(new Label("This goes down"), new Button("Down"), new Button("Further Down")); 
    // 
    //  box.getChildren().addAll(new Label("UserName"), new TextField(), new Button("Connect"), vBox); 

      Scene scene = new Scene(box); 
      primaryStage.setScene(scene); 
      primaryStage.show(); 
     } 

     public static void main(String[] args) { 
      launch(args); 
     } 
    } 




<?xml version="1.0" encoding="UTF-8"?> 
<?import java.lang.*?> 
<?import java.util.*?> 
<?import java.scene.*?> 
<?import java.scene.control.*?> 
<?import java.scene.layout.*?> 

<?import javafx.scene.layout.*?> 
<?import javafx.scene.control.Label?> 
<?import javafx.scene.control.TextField?> 

<HBox> 
    <children> 
     <Label text= "UserName" /> 
     <TextField /> 
     <Button text= "Connect" /> 
    </children> 
</HBox> 

私はそれのおかげで固定しました私はこれがあなたのproblem but Iは、ロードのfxmlのファイルのmy一般的な方法とあなたposted code between see 1つの違いをfix will sureない'm

+0

コードを実行するとどうなりますか?それはエラーや何かを与えるか? – TM00

+0

はい例外を見ることができるように質問を編集しました。この問題を見ていますが、おそらくfxmlファイルが間違ったディレクトリにあるのを見ましたが、そうではありませんでした。 –

+0

プロジェクトファイル構造を表示してください。 'simpleui.fxml'が' SimpleUI'クラスと同じパッケージにある場合、 'FXMLLoader.load(getClass()。getResource(" simpleui.fxml "));'は正しくFXMLファイルをロードします。 – MBec

答えて

0

はpathでmy questionをanswered人々はyouはnamely loaderで:

 HBox box = FXMLLoader.load(getClass().getResource("/hey/src/application/simpleui.fxml")); 

私はnormalyなどFXMLロード:

 AnchorPane page = (AnchorPane) FXMLLoader.load(PreloadController.class.getResource("PreloaderGUI.fxml")); 

そして、私のControllerとFXMLファイルが同じパッケージ内にあることを確認してください。私はgetResourceメソッドがこれらの例の間で同じであると仮定しますが、Stringパスが渡されます。これをfxmlファイルの名前に変更して、動作するかどうか確認してください。

HBox box = FXMLLoader.load(getClass().getResource("simpleui.fxml")); 
+0

FXMLとControllerは同じパッケージに入れてください。 'Controller'の位置はFXMLファイルの' fx:controller = "path.to.Controller" 'から取得されます。 FXMLファイルの読み込みは、クラスパスからの任意のリソースの読み込みと同じです。参照:http://stackoverflow.com/a/2593175/6708868 – MBec

+0

まだ動作しません –

関連する問題