JavaFX Location没有设置错误信息

尝试closures当前场景时遇到问题,并在selectmenuItem时打开另一个场景。 我的主要舞台编码如下:

public void start(Stage primaryStage) throws Exception { primaryStage.setTitle("Shop Management"); FXMLLoader myLoader = new FXMLLoader(getClass().getResource("cartHomePage.fxml")); Pane myPane = (Pane) myLoader.load(); CartHomePageUI controller = (CartHomePageUI) myLoader.getController(); controller.setPrevStage(primaryStage); Scene myScene = new Scene(myPane); primaryStage.setScene(myScene); primaryStage.show(); } 

当程序执行时,它会转到cartHomePage.fxml。 从那里,我可以select去创build产品或创build类别时,菜单项被选中。 这是我的行动事件:

 Stage prevStage; public void setPrevStage(Stage stage){ this.prevStage = stage; } public void gotoCreateCategory(ActionEvent event) throws IOException { Stage stage = new Stage(); stage.setTitle("Shop Management"); FXMLLoader myLoader = new FXMLLoader(getClass().getResource("createCategory.fxml")); Pane myPane = (Pane) myLoader.load(); Scene scene = new Scene(myPane); stage.setScene(scene); prevStage.close(); setPrevStage(stage); stage.show(); } //Method to change scene when menu item create product is on click @FXML public void gotoCreateProduct(ActionEvent event) throws IOException { Stage stage = new Stage(); stage.setTitle("Shop Management"); FXMLLoader myLoader = new FXMLLoader(getClass().getResource("creatProduct.fxml")); Pane myPane = (Pane) myLoader.load(); Scene scene = new Scene(myPane); stage.setScene(scene); prevStage.close(); setPrevStage(stage); stage.show(); } 

不过,我只能切换一次舞台。 例如,我的默认页面是cartHomePage.fxml。 当我运行程序时,首先我要去创build产品阶段。 之后,我再也不能去任何地方了。 错误消息是:

 java.lang.IllegalStateException: Location is not set. and Null Pointer Exception 

我把它关好并传过去之后,我确定了舞台。 我不知道哪个部分出了问题。

提前致谢。

我有这个问题,发现这个职位。 我的问题只是一个文件名问题。

 FXMLLoader(getClass().getResource("/com/companyname/reports/" + report.getClass().getCanonicalName().substring(18).replaceAll("Controller", "") + ".fxml")); Parent root = (Parent) loader.load(); 

我有一个XML,这是所有来自,我已经确定,我的课是相同的fxml文件less于字控制器。

我搞砸了子string,所以path是错误的…当然,我修复了它的工作文件名。

长话短说,我认为问题是文件名不正确或path是错误的。

我得到这个exception,我发现的“解决scheme”是通过Netbeans IDE,简单地说:

  1. 右键单击 – >“清理并生成”
  2. 再次运行项目

我不知道为什么这个工作,但它!

我将简单的NetBeans 8 Java FXML应用程序转换为Maven驱动的应用程序。 然后我遇到了问题,因为getResource()方法无法find.fxml文件。 在我的原始应用程序中,fxmls通过包树分散 – 每个在其控制器类文件旁边。 在我做了Clean并在NetBeans中构build之后,我在目标文件夹中检查了结果.jar – .jar根本没有包含任何fxml。 所有的fxmls都奇怪地消失了。

然后我把所有的fxmls放到resources / fxml文件夹中,并相应地设置getResource方法参数,例如: FXMLLoader(App.class.getClassLoader().getResource("fxml/ObjOverview.fxml")); 在这种情况下,一切都OK了。 fxml文件夹出现在.jar的根目录下,它包含了我所有的fxmls。 该scheme正在按预期工作。

这往往没有得到正确的位置path。 意识到path从代码所在的当前包开始,而不是项目的根。 只要你得到这个相对path是正确的,你应该能够在这种情况下避开这个错误。

我尝试了一个快速简单的事情:

我有两个包 – > app.gui和app.login

在我的login类中,我使用app.gui中的mainview.fxml,所以我在login.fxml中做了这个

FXMLLoader fxmlLoader = new FXMLLoader(getClass()。getResource(“../ gui / MainView.fxml”));

它的工作:)

问候

CsPeitch和其他人的回答是正确的。 只要确保fxml文件被复制到您的类输出目标,或者运行时就不会看到它。 检查生成的类文件目录,看看fxml是否在那里

我在我的JavaFX应用程序中遇到了同样的问题。 更奇怪的是:在我的Windows开发环境中,一切正常,用fxml加载器。 但是当我在Debian机器上执行完全相同的代码时,我发现类似的错误是“未设置位置”。

我在这里看到所有的答案,但似乎没有人真正“解决”这个问题。 我的解决scheme很简单,我希望它能帮助你们中的一些人:

也许Java被getClass()方法弄糊涂了。 如果某些东西在不同的线程中运行,或者类实现了任何接口,那么getClass()方法可能会返回与您的类不同的类。 在这种情况下,您的相对pathcreatProduct.fxml将是错误的,因为你的“是”不在你认为你的path…

所以要保存一边:更具体一些,并尝试使用类的静态类字段请注意YourClassHere.class )。

 @FXML public void gotoCreateProduct(ActionEvent event) throws IOException { Stage stage = new Stage(); stage.setTitle("Shop Management"); FXMLLoader myLoader = new FXMLLoader(YourClassHere.class.getResource("creatProduct.fxml")); Pane myPane = (Pane) myLoader.load(); Scene scene = new Scene(myPane); stage.setScene(scene); prevStage.close(); setPrevStage(stage); stage.show(); } 

意识到这一点后,我会一直这样做。 希望有所帮助!

我知道这是一个迟到的答案,但我希望能帮助有这个问题的其他人。 我得到了同样的错误,发现我不得不在我的文件path前插入一个/ 。 更正的函数调用将是:

 FXMLLoader myLoader = new FXMLLoader(getClass().getResource("/createCategory.fxml")); // ^ 

我的意思是这样的:(对不起,这么久,我在我的电话):

 FXMLLoader myLoader = null; Scene myScene = null; Stage prevStage = null; public void start(Stage primaryStage) throws Exception { primaryStage.setTitle("Shop Management"); myLoader = new FXMLLoader(getClass().getResource("cartHomePage.fxml")); Pane myPane = (Pane) myLoader.load(); CartHomePageUI controller = (CartHomePageUI) myLoader.getController(); controller.setPrevStage(primaryStage); myScene = new Scene(myPane); primaryStage.setScene(myScene); primaryStage.show(); } 

之后

 public void setPrevStage(Stage stage){ this.prevStage = stage; } public void gotoCreateCategory(ActionEvent event) throws IOException { Stage stage = new Stage(); stage.setTitle("Shop Management"); myLoader = new FXMLLoader(getClass().getResource("createCategory.fxml")); Pane myPane = (Pane) myLoader.load(); Scene scene = new Scene(myPane); stage.setScene(scene); // prevStage.close(); I don't think you need this, closing it will set preStage to null put a breakpoint after this to confirm it setPrevStage(stage); stage.show(); } //Method to change scene when menu item create product is on click @FXML public void gotoCreateProduct(ActionEvent event) throws IOException { Stage stage = new Stage(); stage.setTitle("Shop Management"); myLoader = new FXMLLoader(getClass().getResource("creatProduct.fxml")); Pane myPane = (Pane) myLoader.load(); Scene scene = new Scene(myPane); stage.setScene(scene); // prevStage.close(); I don't think you need this, closing it will set preStage to null put a breakpoint after this to confirm it setPrevStage(stage); stage.show(); } 

试试吧,请让我知道。 Lionnel

这发生在我身上,我find了解决办法。 如果你用你的.fxml文件build立你的项目在与启动线的类不同的包( Parent root = FXMLLoader.load(getClass().getResource("filenamehere.fxml")); ),并使用相对path窗户除了从第一个不会启动时,你运行的jar。 为了使它保持简短,将.fxml文件放在与启动它的类相同的包中,并像这样设置path(“filenamehere.fxml”),它应该可以正常工作。

我有同样的问题。

几分钟后,我觉得我正在尝试从错误的位置加载file.fxml。

 FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/[wrong-path]/abc.fxml")); fxmlLoader.setClassLoader(getClass().getClassLoader()); fxmlLoader.setRoot(this); fxmlLoader.setController(this); fxmlLoader.load(); 

我偶然发现了同样的问题。 通过“运行”button,程序运行良好,但不是从我之前导出的可运行JAR中运行。 我的解决scheme是:

1)将Main类移到默认包

2)为Eclipse设置其他path,以及从JAR文件运行其他path(将其粘贴到Main.java中

 public static final String sourcePath = isProgramRunnedFromJar() ? "src/" : ""; public static boolean isProgramRunnedFromJar() { File x = getCurrentJarFileLocation(); if(x.getAbsolutePath().contains("target"+File.separator+"classes")){ return false; } else { return true; } } public static File getCurrentJarFileLocation() { try { return new File(Main.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath()); } catch(URISyntaxException e){ e.printStackTrace(); return null; } } 

然后在启动方法,你必须加载这样的文件:

 FXMLLoader loader = new FXMLLoader(getClass().getResource(sourcePath +"MainScene.fxml")); 

它在eclipse (fx)clipse插件的Eclipse Mars中适用于我。

对于Intellij用户,我的问题是,我有我的fxml文件(src / main / resources)的目录没有标记为“Resources”目录。

打开模块/项目设置转到源选项卡,并确保Intellij知道该目录包含项目资源文件。

我的奇怪… IntelliJ具体的怪癖。

我看着我的输出类,并有一个文件夹:

XYZ

代替

X / Y / Z

但是如果你在IntelliJ中设置了某些选项,那么在导航器中它们都将看起来像xyz

所以检查你的输出文件夹,如果你挠头

我有同样的问题。 这是一个简单的问题,没有指定正确的path。

右键点击你的.fxml文件并select属性(对于使用eclipse的用户来说,对于另一个IDE不会有太大的差别),然后拷贝从/packagename开始的位置直到最后,这样可以解决问题

我认为问题是不正确的布局名称或无效的布局文件path。

对于IntelliJ,您可以创build资源目录并在其中放置布局文件。

 FXMLLoader loader = new FXMLLoader(); loader.setLocation(getClass().getResource("/sample.fxml")); rootLayout = loader.load();