2012-12-09 102 views
2

我是一個相當新的java程序員。我從零開始只有大約五個星期的經驗,並且如果在場景構建器中創建的javafx fxml文件與控制器類不在同一個文件夾中,那麼它們可以正確加載,以便正確加載。從bin文件夾以外的文件夾加載fxml文件時出錯

我使用

Win7x64 running jre7x86 for this build 

Eclipse Juno Service Release 1 
Build id: 20120920-0800 

jre version 1.7.0_07 

javaFx version 2.2.1-b03 

SceneBuilder version 1.0-b50 

我的場景加載程序代碼是

private static final String RESOURCE_PATH = "/resources/"; 
public static Stage buildStage(@SuppressWarnings("rawtypes") Class pClass, 
     String stageTitle, String resourceLocation) 
    { 
     Stage temp = new Stage(); 
     Pane page = null; 
     try 
     { 
     page = FXMLLoader.load(pClass.getResource(RESOURCE_PATH + resourceLocation), null, 
       new JavaFXBuilderFactory()); 
     } 
     catch (IOException e) 
     { 
     e.printStackTrace(); 
     } 
     Scene scene = new Scene(page); 
     temp.setScene(scene); 
     temp.setTitle(stageTitle); 
     temp.setResizable(false); 
     return temp; 
    } 

我知道這個項目目錄是/workspace/projectName/所以理論上裝載機應該能夠找到這些文件,如果它賦予了相對路徑對嗎?我得到的錯誤是

Exception in Application start method 
Exception in thread "main" java.lang.RuntimeException: Exception in Application start method 
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:403) 
    at com.sun.javafx.application.LauncherImpl.access$000(LauncherImpl.java:47) 
    at com.sun.javafx.application.LauncherImpl$1.run(LauncherImpl.java:115) 
    at java.lang.Thread.run(Unknown Source) 
Caused by: java.lang.NullPointerException: Location is required. 
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2737) 
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2721) 
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2707) 
    at outofunit.system.StageFactory.buildStage(StageFactory.java:32) 
    at outofunit.desktop.ArcMaster.start(ArcMaster.java:28) 
    at com.sun.javafx.application.LauncherImpl$5.run(LauncherImpl.java:319) 
    at com.sun.javafx.application.PlatformImpl$5.run(PlatformImpl.java:206) 
    at com.sun.javafx.application.PlatformImpl$4.run(PlatformImpl.java:173) 
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method) 
    at com.sun.glass.ui.win.WinApplication.access$100(WinApplication.java:29) 
    at com.sun.glass.ui.win.WinApplication$3$1.run(WinApplication.java:73) 
    ... 1 more 

我試着給裝載機的絕對路徑,以及,但它只是不接受任何不是直接在同一目錄下。我在這裏抓住吸管試圖弄清楚這一點。

我試圖在我自己的研究,但我沒有得到太多,在這裏似乎似乎相似的幾個答案似乎沒有幫助,或者我太密集和/或經驗不足,沒有意義其中。他們是JavaFX 2.0 loading fxml files with event handlers failsJavaFX 2.0 FXML resource loading error

我的一個朋友是能夠通過使用此代碼

public void load(String pFileName, Stage pStage, String pTitle) 
    { 
     String fName = RESOURCE_PATH + pFileName; 

     try 
     { 
     String externalForm = getClass().getResource(fName) 
      .toExternalForm(); 

     InputStream inStream = new URL(externalForm).openStream(); 

     FXMLLoader loader = new FXMLLoader(); 
     Pane p = (Pane)loader.load(inStream); 

     pStage.setTitle(pTitle); 
     pStage.setScene(new Scene(p)); 

     mWindowControl = loader.getController(); 
     mWindowControl.setUp(pStage); 

     pStage.show(); 
     } 
     catch (Exception e) 
     { 
     e.printStackTrace(); 
     } 
    } 

最大的區別,爲什麼我還沒有使用他的代碼是不斷的原因來克服這個問題,因爲我的根窗格是一個錨窗格,而不是一個正常的窗格,他的方式URL instream強制一個正常的窗格。我是否因爲沒有使用普通窗格作爲我的基礎而過錯?我很樂意提供任何幫助或建議。謝謝。

編輯:所以我一直在處理這個問題,錯誤信息已經改變。

我改變了代碼,這

private final String RESOURCE_PATH = "resources/"; 
public void load(String pFileName, Stage pStage, String pTitle) 
    { 
     String fName = RESOURCE_PATH + pFileName; 

     Pane page = null; 

     FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource(fName)); 

     try 
     { 
     page = (Pane) fxmlLoader.load(); 
     } 
     catch (IOException exception) 
     { 
     throw new RuntimeException(exception); 
     } 

     Scene scene = new Scene(page); 
     pStage.setScene(scene); 
     pStage.setTitle(pTitle); 

     mWindowControl = fxmlLoader.getController(); 
     mWindowControl.setUp(pStage); 

     pStage.show(); 
    } 

但現在我得到的錯誤是

java.lang.IllegalStateException: Location is not set. 
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2021) 
    at outofunit.desktop.WindowLoader.load(WindowLoader.java:136) 
    at outofunit.desktop.WindowLoader.load(WindowLoader.java:62) 
    at outofunit.desktop.ArcMaster.start(ArcMaster.java:30) 
    at com.sun.javafx.application.LauncherImpl$5.run(LauncherImpl.java:319) 
    at com.sun.javafx.application.PlatformImpl$5.run(PlatformImpl.java:206) 
    at com.sun.javafx.application.PlatformImpl$4.run(PlatformImpl.java:173) 
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method) 
    at com.sun.glass.ui.win.WinApplication.access$100(WinApplication.java:29) 
    at com.sun.glass.ui.win.WinApplication$3$1.run(WinApplication.java:73) 
    at java.lang.Thread.run(Unknown Source) 

我不明白是怎麼位置沒有被設置

回答

4

兩個例外情況說「您嘗試加載的(FXML)文件無法在您提供的位置找到」。你的文件名是錯誤的或它的路徑。給你的包結構或者至少包含load(String pFileName, Stage pStage, String pTitle)方法的類文件的路徑以及你想要加載的FXML文件的路徑。閱讀getResource()的javadoc API並在網上查看關於它的示例代碼。

+0

我最終找到了正確的結構爲我的資源文件夾,並得到這個工作。謝謝。 – Samuel

+8

@Samuel,本來很高興分享你如何真正解決這個問題。 – diegoaguilar

+0

@diegoaguilar,我希望我可以張貼我是如何解決這個問題的。但自那時以後,這個項目被ccleaner破壞了,所以我不再擁有它了。 – Samuel

7

這是一個路徑問題,java需要知道從包層次結構中獲取文件的位置。 所以使用"/package1/resourcename.fxml"應該從樹的根部找到源。 由於您已經位於軟件包樹頂部,因此您通常會在開始時關閉"/"

-1

我遇到了同樣的問題。看着我的Jar(用Winzip)並看到FXML文件不在那裏。原來,構建jar文件的我的ANT腳本只包含來自我的項目的bin目錄的* .class文件。

相關問題