2

after I added Maven dependecies to my JavaFX program and created a new package, called Algorithm wth two classes, my application fails to start.

Following Exceptions are thrown:

Caused by: java.lang.RuntimeException: Exception in Application start method Caused by: java.lang.NullPointerException: Location is required.

and so on...

I have tried:
JavaFX "Location is required." even though it is in the same package
and
Exception in Application start method java.lang.reflect.InvocationTargetException

Both didn't work

In the image below is my project structure, any help is highly appreciated. It seems that sample.fxml is not valid...

Edit:
relocating the fxml file in resources directory didn't work either

IMage

Alan
  • 589
  • 5
  • 29
  • Possible duplicate of [How to reference javafx fxml files in resource folder?](https://stackoverflow.com/questions/19602727/how-to-reference-javafx-fxml-files-in-resource-folder) – xxxvodnikxxx Apr 11 '19 at 13:37
  • Also https://stackoverflow.com/questions/22000423/javafx-and-maven-nullpointerexception-location-is-required – Federico klez Culloca Apr 11 '19 at 13:37
  • Already tried it with "\sample.fxml" doesn't work – Alan Apr 11 '19 at 13:39
  • @xxxvodnikxxx indeed I have tried to change the relative path ^ but no change – Alan Apr 11 '19 at 13:41
  • 1
    You need to put _resources_ in the `src/main/resources` directory. The `src/main/java` directory is for _source_ files only. – Slaw Apr 11 '19 at 13:47
  • @Slaw The fxml file,beacause that doesn't work either? – Alan Apr 11 '19 at 16:47
  • Yes. Source files are the `*.java` files, resource files are everything else. Maven requires, by default, that all _production_ resource files to be under `src/main/resources`; if they aren't, then they aren't copied into the build location and thus don't end up on the classpath at runtime. – Slaw Apr 11 '19 at 16:56
  • how are you trying to run this? From IntelliJ or from Maven? – Renato Apr 11 '19 at 16:57
  • When you tried moving it to the `resources` directory, where exactly did you put it? If you put it directly under `resources` then the new path is `"/sample.fxml"`. If you want the FXML file to stay in the same package, mirror the directory structure from `src/main/java`. In other words, put the FXML file in `src/main/resources/sample`. – Slaw Apr 11 '19 at 16:58
  • If you're running Java 9+, you may be facing this issue even if you put the FXML files in `src/main/resources`. https://stackoverflow.com/questions/46861589/accessing-resource-files-from-external-modules – Renato Apr 11 '19 at 17:02
  • @Renato Don't believe that's the issue since (1) image shows no `module-info.java` file and (2) even if modularized we seem to be dealing with a single module (a module can always read its own resources). – Slaw Apr 11 '19 at 17:10
  • All I can say is that this exact same setup works for me as long as the fxml file is on the classpath, which is achieved by putting it in the `src/main/resources` dir. Your problem seems unrelated to JavaFX. – Renato Apr 11 '19 at 17:33
  • @Renato if I put the fxml file in the resources dir, same problem and I try it from IntelliJ – Alan Apr 11 '19 at 17:34
  • @Slaw putting it under res, with the path "/sample.xml" the same problem ocures – Alan Apr 11 '19 at 17:35
  • @Slaw changing the language level to 11 and moving the fxml file in res. dir worked, I don't know why but yes – Alan Apr 11 '19 at 17:39

2 Answers2

2

I've just copied this code approximately, put it on my IntelliJ, and it worked like a charm. Maybe you're just missing the / on the name of the resource?

Here's the exact code I used:

package hello;

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

import java.io.IOException;

public class Hello extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception {
        Parent parent = FXMLLoader.load(Hello.class.getResource("/hello.fxml"));
        Scene scene = new Scene(p, 400, 400);
        primaryStage.setTitle("my app");
        primaryStage.setScene(scene);
        primaryStage.centerOnScreen();
        primaryStage.show();
    }

    public static void main(String[] args) throws IOException {
        launch(Hello.class);
    }
}

The FXML file:

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

<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Text?>
<AnchorPane xmlns="http://javafx.com/javafx"
            xmlns:fx="http://javafx.com/fxml"
            fx:controller="hello.Hello"
            prefHeight="400.0" prefWidth="600.0">
    <VBox>
        <Text>Hello</Text>
    </VBox>
</AnchorPane>

Run with IntelliJ. Works. Create a Jar, run with java -jar... also works.

Renato
  • 12,940
  • 3
  • 54
  • 85
  • Try creating a new Java project in IntelliJ without Maven. Put the java file above in `src/`. Create a new `resources` dir, mark it as `resources root` (right-click on it and select the option). Just run it. It works!!! I can guarantee :) Tried with Java 8 to avoid module stuff. – Renato Apr 11 '19 at 17:38
  • Hello I did it some minutes ago, now I did it again after changing the language level to 11 Thanks a ton – Alan Apr 11 '19 at 17:38
  • Excellent! So what was the problem exaclty? – Renato Apr 11 '19 at 17:39
  • I don't know it seems that language level 6 caused the problem (Not sure about that) Anyways what's the explanation of your solution? – Alan Apr 11 '19 at 17:40
  • Java 6 does not have JavaFX :) It was added on Java 8 I think. Before that, it was distributed separately. Now, with 11, again, it's been removed from the std distribution of the JDK... – Renato Apr 11 '19 at 17:41
  • Oh okay thanks! And why did I have to move the file in res dir? :) – Alan Apr 11 '19 at 17:45
  • Because that's where tools like Maven and IntelliJ expect to find resources (non-java files used by code). See https://maven.apache.org/guides/introduction/introduction-to-the-standard-directory-layout.html – Renato Apr 11 '19 at 19:43
0

Your IDE, IntelliJ, loads resources files from the classpath using pattern recognition to match the file extensions. By default, IntelliJ includes extensions like JPG, PNG, HTML, etc...

This means you need to manually specify the file extensions you want to compiler to find, for you that's FXML.

Here are some simple instructions on how to achieve this: https://www.jetbrains.com/help/idea/working-with-projects.html

NFDev
  • 68
  • 8
  • 2
    Please, don't just link the solution. Explain it a bit so that the link is only complementary to your answer and not absolutely necessary. – Federico klez Culloca Apr 11 '19 at 13:45
  • 1
    yes, exactly, this could be the comment, not an answer, just imagine- official doc will be moved / deleted / have new structure, then you will find this post, but dead link .. Please, always try to put solution directly into posts. – xxxvodnikxxx Apr 11 '19 at 13:50
  • 1
    I appreciate the criticism, you're right, hope my edit helps. – NFDev Apr 11 '19 at 13:53
  • What do I have to do? The link didn't tell my answer – Alan Apr 11 '19 at 16:51
  • When using Maven, don't configure the IDE, configure Maven. See e.g. https://stackoverflow.com/questions/9063296/how-can-i-write-maven-build-to-add-resources-to-classpath – Renato Apr 11 '19 at 17:00