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:55

If you look at the docs [1], you see that the load() method can take a URL:

load(URL location)

So if you're running Java 7 or newer, you can load the FXML file like this:

URL url = Paths.get("./src/main/resources/fxml/Fxml.fxml").toUri().toURL();
Parent root = FXMLLoder.load(url);

This example is from a Maven project, which is why the FXML file is in the resources folder.

[1] https://docs.oracle.com/javase/8/javafx/api/javafx/fxml/FXMLLoader.htm

查看更多
旧时光的记忆
3楼-- · 2019-01-02 19:01
URL url = new File("src/main/java/ua/adeptius/goit/sample.fxml").toURL();
Parent root = FXMLLoader.load(url);

That is helped for me because

getClass.getResource("path")

allways returns me null;

查看更多
路过你的时光
4楼-- · 2019-01-02 19:03

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")
查看更多
情到深处是孤独
5楼-- · 2019-01-02 19:05

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

" fxml_example.fxml" instead of "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.

查看更多
临风纵饮
6楼-- · 2019-01-02 19:07

I simply passed the loader a proper URL like this:

URL url = getClass().getResource("myForm.fxml");
Parent root = FXMLLoader.load(url);
查看更多
牵手、夕阳
7楼-- · 2019-01-02 19:09

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:

    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.

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

  • "/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

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

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

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.

Related:

查看更多
登录 后发表回答