我是一個相當新的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 fails和JavaFX 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)
我不明白是怎麼位置沒有被設置
我最終找到了正確的結構爲我的資源文件夾,並得到這個工作。謝謝。 – Samuel
@Samuel,本來很高興分享你如何真正解決這個問題。 – diegoaguilar
@diegoaguilar,我希望我可以張貼我是如何解決這個問題的。但自那時以後,這個項目被ccleaner破壞了,所以我不再擁有它了。 – Samuel