2017-07-25 15 views
1

JavaFXを使用して経理会社のアプリケーションを作成しています。私はFXMLで設定したボタンをクリックすると奇妙なエラーが発生します。ここでJavaFXアプリケーションで「java.lang.IllegalStateException:ロケーションが設定されていません」

java.lang.IllegalStateException: Location is not set. 
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2434) 
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2409) 

は私のMain.javaコードです:ここで

package tech.faraaz.zoforo; 

import javafx.application.Application; 
import javafx.fxml.FXMLLoader; 
import javafx.scene.Parent; 
import javafx.scene.Scene; 
import javafx.stage.Stage; 

public class Main extends Application { 

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

     Scene scene = new Scene(root); 

     stage.setTitle("Zoforo"); 
     stage.setScene(scene); 
     stage.show(); 
    } 


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

は私のスプラッシュ画面のコードです:ここで

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

<?import javafx.scene.control.Button?> 
<?import javafx.scene.control.Label?> 
<?import javafx.scene.control.ListView?> 
<?import javafx.scene.control.TextField?> 
<?import javafx.scene.image.Image?> 
<?import javafx.scene.image.ImageView?> 
<?import javafx.scene.layout.AnchorPane?> 

<AnchorPane maxHeight="410.0" maxWidth="410.0" minHeight="410.0" minWidth="410.0" prefHeight="400.0" prefWidth="401.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="tech.faraaz.zoforo.gui.SplashScreenController"> 
    <children> 
     <ImageView fitHeight="150.0" fitWidth="200.0" layoutX="24.0" layoutY="23.0" pickOnBounds="true" preserveRatio="true"> 
     <image> 
      <Image url="@../assets/logo.png" /> 
     </image> 
     </ImageView> 
     <ListView layoutX="24.0" layoutY="158.0" prefHeight="200.0" prefWidth="200.0" /> 
     <Button layoutX="248.0" layoutY="172.0" mnemonicParsing="false" onMouseClicked="#openAccountWindow" prefHeight="27.0" prefWidth="139.0" text="Open Account" /> 
     <Button layoutX="248.0" layoutY="209.0" mnemonicParsing="false" prefHeight="27.0" prefWidth="139.0" text="New Account" /> 
     <Button layoutX="248.0" layoutY="313.0" mnemonicParsing="false" prefHeight="27.0" prefWidth="139.0" text="Scan Statement" /> 
     <Button layoutX="248.0" layoutY="276.0" mnemonicParsing="false" prefHeight="27.0" prefWidth="139.0" text="Open Statement" /> 
     <Label layoutX="24.0" layoutY="374.0" text="Copyright Zoforo 2017 | zoforo.com" /> 
     <TextField layoutX="24.0" layoutY="122.0" promptText="Search Profiles..." /> 
    </children> 
</AnchorPane> 

はSplashScreenController.java内のコードです。私は "ActionEvent"の代わりに "Event"を使用しています。

@FXML 
public void openAccountWindow(Event event) throws Exception { 

    try { 
     FXMLLoader loader = new FXMLLoader(getClass().getResource("gui/OpenAccountScreen.fxml")); 
     Parent root = (Parent) loader.load(); 

     Stage stage = new Stage(); 
     stage.setScene(new Scene(root)); 
     stage.show(); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 

OpenAccountScreen.javaのコードは次のとおりです。

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

<?import javafx.scene.control.Button?> 
<?import javafx.scene.control.TableColumn?> 
<?import javafx.scene.control.TableView?> 
<?import javafx.scene.control.TextField?> 
<?import javafx.scene.image.Image?> 
<?import javafx.scene.image.ImageView?> 
<?import javafx.scene.layout.AnchorPane?> 


<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="384.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1"> 
    <children> 
     <ImageView fitHeight="71.0" fitWidth="169.0" layoutX="14.0" layoutY="14.0" pickOnBounds="true" preserveRatio="true"> 
     <image> 
      <Image url="@../assets/logo.png" /> 
     </image> 
     </ImageView> 
     <TableView layoutX="14.0" layoutY="137.0" prefHeight="200.0" prefWidth="572.0"> 
     <columns> 
      <TableColumn prefWidth="204.0" text="Account Name" /> 
      <TableColumn minWidth="0.0" prefWidth="218.0" text="Account Number" /> 
      <TableColumn minWidth="8.0" prefWidth="149.0" text="Statements" /> 
     </columns> 
     </TableView> 
     <Button layoutX="480.0" layoutY="346.0" mnemonicParsing="false" text="Open Account" /> 
     <TextField layoutX="14.0" layoutY="101.0" promptText="Search Accounts..." /> 
    </children> 
</AnchorPane> 

助けていただければ幸いです。ありがとう。自分のメインクラスで

+2

これまでに「google:location not set javafx」と聞いてきました。実際のエラーは、表示されないコード(たとえば、 'tech.faraaz.zoforo.gui.SplashScreenController'、またはボタンクリックでトリガーするコードなど)にある可能性があります。また、「ボタンをクリックする」というエラーが表示されます。 (私はそれが "Open Account"だと思います)、エラーは 'openAccountWindow'で生成されます。 – jewelsea

+0

ここにSplashScreenControllerのコードを示します。私はすでに手前でその質問をチェックしていて、解決策を見つけることができませんでした。 @jewelsea '@FXML ます。public void openAccountWindow(イベントイベント)が例外{ 試し{ FXMLLoaderローダー=新しいFXMLLoader(。はgetClass()のgetResource( "GUI/OpenAccountScreen.fxmlを"))スロー; 親ルート=(親)loader.load(); ステージステージ=新しいステージ(); stage.setScene(新しいシーン(ルート)); stage.show(); } catch(例外e){ e.printStackTrace(); } } – faraaz

+1

質問を編集して、コメントではなく質問にコードを入れてください。また、あなたが提供したコードを見ると、おそらくOpenAccountScreen.fxmlまたはそのコントローラにエラーがあるので、おそらくそれらを提供する必要があります。この種の問題の迅速な回答を得る最善の方法は、誰かがコピーして貼り付けて変更を加えずに問題を即座に複製できる[mcve]を提供することです。関連する質問やリソースを既にチェックしていて問題を解決できなかった場合は、あなたの質問にもリンクを付けてください。 – jewelsea

答えて

2

、あなたのパッケージおよびクラス名は以下のとおりです。SplashScreenControllerで

tech.faraaz.zoforo.Main 

それは次のとおりです。

tech.faraaz.zoforo.gui.SplashScreenController 

ので、これらのクラスは異なるパッケージです。

しかし、同じ相対的な場所を使用してリソースを取得しようとします。メインで

:SplashScreenControllerで

getClass().getResource("gui/SplashScreen.fxml") 

:それが発見されるのをSplashControllerの場所へ

getClass().getResource("gui/OpenAccountScreen.fxml") 

ので相対的、そしてOpenAccountScreen.fxmlは次の場所にする必要があります:

tech/faraaz/zoforo/gui/gui/OpenAccountScreen.fxml 

私はそこにはないと確信しています...おそらく、現在のクラスに関連してFXMLにアクセスするのではなく、特定のクラス(たとえば、クラス)に関連してアクセスする必要があります。常にメイン)または絶対参照を介して。それはあなたの混乱を防ぐのに役立つかもしれません。

Main.class.getResource("gui/SplashScreen.fxml"); 
Main.class.getResource("gui/OpenAccountScreen.fxml"); 

をOR:

例えば、書き込み

getClass().getResource("/tech/faraaz/zoforo/gui/SplashScreen.fxml") 
getClass().getResource("/tech/faraaz/zoforo/gui/OpenAccountScreen.fxml") 

注意を、このようなデバッグものに、あなたは常にだけで実行できます。

System.out.println(getClass().getResource("gui/OpenAccountScreen.fxml")); 

を、それがnullを印刷した場合リソースが期待どおりの場所にないことがわかっている場合は、そこからトラブルシューティングを行うことができます。

+0

ご協力いただきありがとうございます。大変感謝しております。 – faraaz

関連する問題