JavaFX“需要位置。” 即使它在同一个包中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20507591/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 02:24:47  来源:igfitidea点击:

JavaFX "Location is required." even though it is in the same package

javajavafx-2

提问by Gremash

I am trying to get my JavaFX program to run but am having some difficulty. I keep getting an error of 'java.lang.NullPointerException: Location is required.' The fxml file is in the same package as Application class. Here is my very simple code:

我试图让我的 JavaFX 程序运行,但遇到了一些困难。我不断收到“java.lang.NullPointerException: Location is required”的错误消息。fxml 文件与 Application 类在同一个包中。这是我非常简单的代码:

package com.kromalights.designer.entry;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception{
        Parent root = FXMLLoader.load(getClass().getResource("main.fxml"));
        primaryStage.setTitle("Kromalights Designer");
        primaryStage.setScene(new Scene(root, 300, 275));
        primaryStage.show();
    }


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

And here is a copy of my main.fxml file:

这是我的 main.fxml 文件的副本:

<?xml version="1.0" encoding="UTF-8"?>

<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.BorderPane?>
<?scenebuilder-stylesheet mailStyles.css?>
<?import java.net.*?>

<BorderPane prefHeight="300.0" prefWidth="300.0" xmlns:fx="http://javafx.com/fxml/1"
        xmlns="http://javafx.com/javafx/2.2"
        fx:controller="com.kromalights.designer.entry.Controller">
    <bottom>
        <Pane prefHeight="200.0" prefWidth="200.0"/>
    </bottom>
    <center>
        <Pane prefHeight="200.0" prefWidth="200.0"/>
    </center>
    <left>
        <VBox prefHeight="200.0" prefWidth="100.0"/>
    </left>
    <top>
        <HBox prefHeight="100.0" prefWidth="200.0"/>
    </top>
    <stylesheets>
        <URL value="@mainStyles.css" />
    </stylesheets>
</BorderPane>

The controller class does exist and is in the package specified in the fxml file. All of my names are correct and are where I think they should be. What am I missing? I did try renaming my fxml file in case it was a name issue. Please help. FYI, I am using Intellij IDEA on OSX.

控制器类确实存在并且位于 fxml 文件中指定的包中。我所有的名字都是正确的,是我认为应该出现的地方。我错过了什么?我确实尝试重命名我的 fxml 文件,以防它是名称问题。请帮忙。仅供参考,我在 OSX 上使用 Intellij IDEA。

UPDATE: This is a Maven issue. I setup Maven for this project and that caused the issue. I removed Maven temporarily so I can continue working without it. Does anyone have any insight as to how I would best handle this when using Maven?

更新:这是一个 Maven 问题。我为这个项目设置了 Maven,这导致了这个问题。我暂时删除了 Maven,这样我就可以在没有它的情况下继续工作。有没有人知道我在使用 Maven 时如何最好地处理这个问题?

采纳答案by Gremash

Moving the file to the main/resources directory worked.

将文件移动到 main/resources 目录有效。

回答by Pedro Borges

If your project is Maven based Idea will ignore all but java files in your src/main/java folder. Like you said moving the file to src/main/resources works, but now SceneBuilder does not know about your controller class anymore because it's in a completely different directory and you lose all the insight, you build blindly.

如果您的项目是基于 Maven 的,Idea 将忽略 src/main/java 文件夹中除 java 文件之外的所有文件。就像你说将文件移动到 src/main/resources 是可行的,但现在 SceneBuilder 不再知道你的控制器类了,因为它在一个完全不同的目录中,你失去了所有的洞察力,你盲目地构建。

I believe this is an issue in intellij since SceneBuilder EXPECTSto find your controller class java file in (and i quote) "the same directory, a direct subdirectory or the parent directory of the .fxml" document.

我相信这是 intellij 中的一个问题,因为 SceneBuilder希望在(我引用)“相同目录、直接子目录或 .fxml”文档的父目录中找到您的控制器类 java 文件。

回答by will

I've seen this error a few times now. So often that I wrote a small project, called "Simple" with a Netbeans Maven FXML application template just to go back to as a kind of 'reference model' when things go askew. For testing, I use something like this:

我现在已经看到这个错误几次了。我经常使用 Netbeans Maven FXML 应用程序模板编写一个名为“Simple”的小项目,只是为了在出现问题时作为一种“参考模型”返回。为了测试,我使用这样的东西:

    String sceneFile = "/fxml/main.fxml";
    Parent root = null;
    URL    url  = null;
    try
    {
        url  = getClass().getResource( sceneFile );
        root = FXMLLoader.load( url );
        System.out.println( "  fxmlResource = " + sceneFile );
    }
    catch ( Exception ex )
    {
        System.out.println( "Exception on FXMLLoader.load()" );
        System.out.println( "  * url: " + url );
        System.out.println( "  * " + ex );
        System.out.println( "    ----------------------------------------\n" );
        throw ex;
    }

When you run that snippet and the load fails, you should see a reason, or at least a message from the FXMLLoader. Since it's a test, I throw the exception. You don't want to continue.

当您运行该代码段并且加载失败时,您应该会看到一个原因,或者至少是来自 FXMLLoader 的一条消息。因为是测试,所以抛出异常。你不想继续。

Things to note. This is a maven project so the resources will be relative to the resources folder, hence:

注意事项。这是一个 Maven 项目,因此资源将相对于资源文件夹,因此:

  • "/fxml/main.fxml".
  • The leading slash is required.
  • The resource passed to the FXMLLoader is case-sensitive:

    // If you load "main.fxml" and your file is called: "Main.fxml"
    // You will will see the message ...
    
    java.lang.NullPointerException: Location is required.
    
  • If you get past that "location is required" issue, then you may have a problem in the FXML

    // Something like this: // javafx.fxml.LoadException: file:/D:/sandbox/javafx/app_examples/person/target/person-00.00.01-SNAPSHOT.jar!/fxml/tableWithDetails.fxml:13

  • “/fxml/main.fxml”。
  • 前导斜杠是必需的。
  • 传递给 FXMLLoader 的资源区分大小写

    // If you load "main.fxml" and your file is called: "Main.fxml"
    // You will will see the message ...
    
    java.lang.NullPointerException: Location is required.
    
  • 如果您解决了“需要位置”的问题,那么您可能在 FXML 中遇到了问题

    // 像这样: // javafx.fxml.LoadException: file:/D:/sandbox/javafx/app_examples/person/target/person-00.00.01-SNAPSHOT.jar!/fxml/tableWithDetails.fxml:13

Will mean that there's a problem on Line 13, in the file, per:

将意味着第 13 行存在问题,在文件中,每个:

  • tableWithDetails.fxml :13
  • tableWithDetails.fxml :13

In the message. At this point you need to read the FXML and see if you can spot the problem. You could try some of the tips in the related question.

消息中。此时,您需要阅读 FXML 并查看是否可以发现问题。您可以尝试相关问题中的一些提示。

For this problem, my opinion is that the file name was proper case: "Main.fxml". When the file was moved the name was probably changed or the string retyped. Good luck.

对于这个问题,我认为文件名是正确的大小写:“Main.fxml”。移动文件时,名称可能已更改或字符串已重新键入。祝你好运。

Related:

有关的:

回答by Evaldas Ilginis

I was getting the same error. In my case there was a leading space-symbol in the fxml file name:

我遇到了同样的错误。在我的情况下,fxml 文件名中有一个前导空格符号:

" fxml_example.fxml" instead of "fxml_example.fxml"

" fxml_example.fxml" 而不是 "fxml_example.fxml"

I don't know where it came from. It was very difficult to notice it. When I removed the leading space, everything went ok. I didn't even knew that file name could start with the space-symbol.

我不知道它是从哪里来的。很难注意到它。当我删除前导空格时,一切正常。我什至不知道文件名可以以空格符号开头。

回答by Amir Arad

In my case all of the above were not the problem at all.

就我而言,上述所有问题都不是问题。

My problem was solved when I replaced :

当我更换时,我的问题解决了:

getClass().getResource("ui_layout.fxml")

with :

和 :

getClass().getClassLoader().getResource("ui_layout.fxml")

回答by mroesler

If your problem is not Maven related and you get the same NullPointerException running this in a browser (while running it from the IDE is fine), try this:

如果您的问题与 Maven 无关,并且您在浏览器中运行它时遇到相同的 NullPointerException(从 IDE 运行它很好),请尝试以下操作:

-> Netbeans -> Right-Click Project -> Properties -> Build -> Deployment -> Check "Request unrestricted access (Enable signing, self-signed)"

This is due to the @FXML Annotation needing permission to inject the value from the FXML markup (see http://docs.oracle.com/javafx/2/fxml_get_started/fxml_deployment.htm)

-> Netbeans -> 右键单击​​项目 -> 属性 -> 构建 -> 部署 -> 勾选“请求无限制访问(启用签名,自签名)”

这是因为@FXML 注释需要从 FXML 标记注入值的权限(请参阅 http://docs.oracle.com/javafx/2/fxml_get_started/fxml_deployment.htm

I was running my App on JDK8u31 and it would never run without the certificate in Chrome / Firefox / IE.

我在 JDK8u31 上运行我的应用程序,如果没有 Chrome/Firefox/IE 中的证书,它永远不会运行。

回答by gronostaj

This problem can be caused by incorrect path to the FXML file.

此问题可能是由 FXML 文件的错误路径引起的。

If you're using absolute paths (my/package/views/view.fxml), you have to precede then with a slash:

如果您使用的是绝对路径 ( my/package/views/view.fxml),则必须在其前面加上斜杠:

getClass().getResource("/my/package/views/view.fxml")

You can use relative paths as well:

您也可以使用相对路径:

getClass().getResource("views/view.fxml")

回答by budo

I had sometimes the same Exception.

我有时也有同样的例外。

When i create a FXML-File with the Wizard in Eclipse, i write example.fxmlin the name field. Eclipse creates a file, like example.fxml.fxml. With this mistake, the FXMLLoader can't find the right FXML-File. So, my tip, check the name of the FXML-Filename in your start-Method and the real Name of File.

当我在 Eclipse 中使用向导创建 FXML 文件时,我在名称字段中写入example.fxml。Eclipse 会创建一个文件,例如example.fxml.fxml。由于这个错误,FXMLLoader 无法找到正确的 FXML 文件。所以,我的建议是,检查你的 start-Method 中 FXML-Filename 的名称和文件的真实名称。

Hopes i could help. Good luck.

希望我能帮上忙。祝你好运。

回答by Adeptius

URL url = new File("src/main/java/ua/adeptius/goit/sample.fxml").toURI().toURL();
Parent root = FXMLLoader.load(url);

That is helped for me because

这对我有帮助,因为

getClass.getResource("path")

always returns me null;

总是给我返回空;

回答by alexkov

I couldn't use

我无法使用

getClass().getResource("views/view.fxml")

because I put my controller class into "controllers" package, so here is my solution:

因为我把我的控制器类放到了“控制器”包中,所以这是我的解决方案:

getClass().getResource("../views/view.fxml")