JavaFX位置未设置错误消息

时间:2013-06-21 05:49:22

标签: java nullpointerexception javafx stage

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

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。从那里,我可以选择在选择菜单项时创建产品或创建类别。这是我的行动事件:

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

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

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

提前致谢。

21 个答案:

答案 0 :(得分:44)

我遇到了这个问题并发现了这篇文章。我的问题只是文件名问题。

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

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

我有一个xml,这都来自我,我确保我的类与fxml文件相同,而不是单词控制器。

我搞砸了子串,所以路径错了......确定它修复了它起作用的文件名后就确定了。

总而言之,我认为问题是文件名不正确或路径错误。

此外: 我已经搬到了Maven项目。非Maven方式是将所有内容都放在项目路径中。下面的答案中列出的Maven方式在开始时有点令人沮丧,但我对代码进行了如下更改:

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

答案 1 :(得分:23)

我得到了这个例外,我找到的“解决方案”是通过Netbeans IDE,简单地说:

  1. 右键单击 - > “清洁和建设”
  2. 再次运行项目
  3. 我不知道 为什么 这有效,但确实如此!

答案 2 :(得分:15)

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

然后我将所有fxmls放入resources / fxml文件夹并相应地设置getResource方法参数,例如:FXMLLoader(App.class.getClassLoader().getResource("fxml/ObjOverview.fxml")); 在这种情况下一切都很好。 fxml文件夹出现在.jar的根目录中,它包含我所有的fxmls。该计划按预期进行。

答案 3 :(得分:11)

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

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

答案 4 :(得分:4)

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

我有两个包 - > app.guiapp.login

在我的登录课程中,我使用mainview.fxml中的app.gui,所以我在login.fxml

中执行了此操作
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("../gui/MainView.fxml"));

它有效:)

答案 5 :(得分:4)

FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("../view/Main.fxml")); 

就我而言,我只是删除..

FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/view/Main.fxml")); 

答案 6 :(得分:3)

这通常无法使位置路径正确。重要的是要意识到路径从代码所在的当前包开始,而不是从项目的根开始。只要你使这个相对路径正确,你就应该能够在这种情况下避开这个错误。

答案 7 :(得分:3)

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

对于IntelliJ,您可以在那里创建资源目录并放置布局文件。

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

答案 8 :(得分:2)

我的JavaFX应用程序中遇到了同样的问题。更奇怪的是:在我的Windows开发环境中,fxml加载器的一切工作正常。但是当我在Debian maschine上执行完全相同的代码时,我得到了类似的错误" location not set"。

我在这里阅读了所有答案,但似乎没有一个真正地解决了#34;问题。我的解决方案很简单,我希望它可以帮助你们中的一些人:

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

所以要保存一边:更具体一点,在你的班级上尝试使用静态类字段注意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();      
}

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

答案 9 :(得分:1)

我的意思是这样的:

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();      
}

试试看,请告诉我。

答案 10 :(得分:1)

CsPeitch和其他人的回答是正确的。只需确保将fxml文件复制到类输出目标,否则运行时将无法看到它。检查生成的类文件目录,看看fxml是否存在

答案 11 :(得分:1)

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

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

答案 12 :(得分:0)

这对我很有效:

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

    @Override
    public void start(Stage primaryStage) throws IOException {

        FXMLLoader loader = new FXMLLoader(getClass().getResource("/fxml/TestDataGenerator.fxml"));
        loader.setClassLoader(getClass().getClassLoader());
        Parent root = loader.load();
        primaryStage.setScene(new Scene(root));
        primaryStage.show();
    }

答案 13 :(得分:0)

我也遇到了同样的问题,我在控制器类中将FXML名称改为FXML文件,问题解决了。

答案 14 :(得分:0)

我遇到了同样的问题。这是一个没有指定正确路径的简单问题。

右键单击/packagename文件上的内容,然后选择属性(对于那些使用eclipse的人来说,对于另一个IDE而言差异很大)然后将副本从{{1}}复制到该位置。结束,那应该解决问题

答案 15 :(得分:0)

我很奇怪...... IntelliJ特有的怪癖。

我查看了输出类,并且有一个文件夹:

X.Y.Z

而不是

X / Y / Z

但如果您在IntelliJ中设置了某些选项,则在导航器中它们看起来都像x.y.z

所以,如果你正在摸索,请检查你的输出文件夹

答案 16 :(得分:0)

我偶然发现了同样的问题。程序在Eclipse中运行良好,通过" Run"按钮,但不是我之前导出的可运行的JAR。 我的解决方案是:

1)将类移至默认包

2)在从JAR文件运行时为Eclipse和其他路径设置其他路径    (将其粘贴到 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;
        }
}

然后在 start 方法中,您必须加载如下文件:

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

它适用于Eclipse Mars中的 e(fx)clipse 插件。

答案 17 :(得分:0)

我遇到了同样的问题。

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

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

答案 18 :(得分:0)

发生在我身上,我找到了解决方案。如果您使用具有启动线的类的不同包中的.fxml文件构建项目 (Parent root = FXMLLoader.load(getClass().getResource("filenamehere.fxml"));) 并使用相对路径你的窗户除了从第一个窗口运行jar时不会启动。 为了简短起见,将.fxml文件与启动它的类放在同一个包中,并设置这样的路径(“filenamehere.fxml”),它应该可以正常工作。

答案 19 :(得分:-1)

在我的情况下,错误的原因是我相应的Java类中的运行时错误。我修好了,一切都很好。 我的提示:不要搜索“位置未设置”

答案 20 :(得分:-1)

我曾经遇到过他类似的问题,但是一旦我重命名了文件,它就解决了,所以我建议您应该

  

“只需​​重命名文件”