JavaFX 位置未设置错误消息 [英] JavaFX Location is not set error message

查看:39
本文介绍了JavaFX 位置未设置错误消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在尝试关闭当前场景并在选择 menuItem 时打开另一个场景时遇到问题.我的主要阶段编码如下:

I have problem when trying to close current scene and open up another scene when menuItem is selected. My main stage is coded as below:

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.From there, I can select to go to create product or create category when the menu item is selected.这是我的行动事件:

When the program is executed, it will go to the cartHomePage.fxml. From there, I can select to go to create product or create category when the menu item is selected. Here is my action event:

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.当我运行程序时,首先我去创建产品阶段.在那之后,我再也不能去任何地方了.错误信息是:

However, I can only switch the stage once. For example, my default page is cartHomePage.fxml. When I run the program, first I go to create product stage. After that, I cannot go to anywhere any more. The error message is:

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

我在关闭它并传递它之后确实设置了舞台.我想知道哪个部分出了问题.

I did set the stage after I close it and pass it around. I wonder which part went wrong.

提前致谢.

推荐答案

我遇到了这个问题并找到了这篇文章.我的问题只是文件名问题.

I had this problem and found this post. My issue was just a file name issue.

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

Parent root = (Parent) loader.load();

我有一个 xml,这一切都来自,我确保我的类与 fxml 文件相同,但没有控制器这个词.

I have an xml that this is all coming from and I have made sure that my class is the same as the fxml file less the word controller.

我弄乱了子字符串,所以路径是错误的......在我修复了它工作的文件名之后果然如此.

I messed up the substring so the path was wrong...sure enough after I fixed the file name it worked.

长话短说,我认为问题在于文件名命名不正确或路径错误.

补充:从那以后,我搬到了一个 Maven 项目.非 Maven 方法是将所有内容都包含在项目路径中.下面的答案中列出的 Maven 方式在开始时有点令人沮丧,但我对代码进行了如下更改:

ADDITION: I have since moved to a Maven Project. The non Maven way is to have everything inside of your project path. The Maven way which was listed in the answer below was a bit frustrating at the start but I made a change to my code as follows:

FXMLLoader loader = new FXMLLoader(ReportMenu.this.getClass().getResource("/fxml/" + report.getClass().getCanonicalName().substring(18).replaceAll("Controller", "") + ".fxml"));

这篇关于JavaFX 位置未设置错误消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆