JavaFX “Location is required.” even though it is i

2019-01-02 18:14发布

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:

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:

<?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.

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?

标签: java javafx-2
15条回答
时光乱了年华
2楼-- · 2019-01-02 18:47

instead of

Parent root = FXMLLoader.load(getClass().getResource("main.fxml"));

put

Parent root = FXMLLoader.load(getClass().getResource("/main.fxml"));

Difference is of " / " .

Reason :

1st one : getResource will try to find the resource relative to the package

2nd one : getResource will treat it as an absolute path and simply call the classloader .

查看更多
柔情千种
3楼-- · 2019-01-02 18:47

I had sometimes the same Exception.

When i create a FXML-File with the Wizard in Eclipse, i write example.fxml in 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.

Hopes i could help. Good luck.

查看更多
浪荡孟婆
4楼-- · 2019-01-02 18:48

The root directory for the loader is in the 'resources' folder for a maven project. So if you have src/main/java then the fxml file path should start from: src/main/resources

https://maven.apache.org/guides/introduction/introduction-to-the-standard-directory-layout.html

查看更多
无与为乐者.
5楼-- · 2019-01-02 18:48

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.

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

查看更多
无色无味的生活
6楼-- · 2019-01-02 18:50

I have found all of the above solutions work just fine because they trigger your IDE to rebuild the project. No magic involved.

The "real" solution for this issue is that you should just ditch your previous build directory.

  • If you are using Maven, just delete the target folder (or do a mvn clean)!
  • If you're using ANT, delete that respective folder (I think it's called out)!
  • If you're using Gradle, delete the build folder!
查看更多
荒废的爱情
7楼-- · 2019-01-02 18:55

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:

-> 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 )

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

查看更多
登录 后发表回答