2

I know, there are lots of similar question around here with similar problems, but after reading tons of posts, I cannot simply solve this issue. Under Compiler > Resource Patterns, I've already put this string "*.fxml". Moreover, I've marked the resources folder as the Resources Root.

Here you can see my project structure:

Project Structure

This is my MainView.java class.

package dataParser.View;

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


public class MainView extends Application {
    private Stage primaryStage;

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

    @Override
    public void start(Stage primaryStage) throws Exception {
        this.primaryStage = primaryStage;

        FXMLLoader loader = new FXMLLoader();
        System.out.println(this.getClass().getResource("/MainView.fxml"));
        loader.setLocation(getClass().getResource("/MainView.fxml"));

        MainViewController mainViewController = new MainViewController();
        mainViewController.setMainApp(this);

        loader.setController(mainViewController);
        Parent layout = loader.load();

        Scene scene = new Scene(layout);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    Stage getPrimaryStage() {
        return primaryStage;
    }

}

Still, I'm just getting back a null resource. Any hints?
Thank you!

EDIT
This is the complete stack-trace. I've also noticed that I've issues linking all my resource files.

    Exception in thread "main" java.lang.RuntimeException: Exception in Application start method
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:973)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:198)
    at java.base/java.lang.Thread.run(Thread.java:844)
Caused by: java.lang.IllegalStateException: Location is not set.
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2459)
    at javafx.fxml/javafx.fxml.FXMLLoader.load(FXMLLoader.java:2435)
    at dataParser.View.MainView.start(MainView.java:29)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:919)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$11(PlatformImpl.java:449)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$9(PlatformImpl.java:418)
    at java.base/java.security.AccessController.doPrivileged(Native Method)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:417)
    at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:175)
    ... 1 more
Davide3i
  • 1,035
  • 3
  • 15
  • 35
  • 1
    Compile to a jar, open it, and look whether the MainView.fxml is there. On principle (will not help) use `MainView.class.getResource` instead of `getClass().getResource`. Use a **maven** build infrastructure for your project, will have correct settings out of the box: src/main/resources and such. – Joop Eggen Dec 22 '17 at 10:55
  • Hi. I'm using Gradle and it set up everything for me. Moreover the .fxml file is into the generated .jar file. – Davide3i Dec 22 '17 at 11:00
  • Gradle provides a standard too. Starting the jar on command line? I had the hope there would be a `/MainView.fxml.xml`, `/resources/MainView.fxml` or such but alas. One time I had a `í` instead of an `i` in the file name. Reenter the string `"/MainView.fxml"` maybe there is some invisible garbage. Because actually there cannot be much wrong. – Joop Eggen Dec 22 '17 at 11:29
  • At the moment I'm simply building and running the program through IntelliJ Idea. I cannot simply understand what the problem can be. I tried to rewrite the string and also to follow the hint in this post (https://stackoverflow.com/questions/20507591/javafx-location-is-required-even-though-it-is-in-the-same-package) where they said to put the .fxml files inside a custom /fxml folder. This issue is driving me nuts. – Davide3i Dec 22 '17 at 13:16
  • 2
    Print out URL returned by this.getClass().getResource("./) then check if your *.fxml fiels end up in same directory. – user158037 Dec 22 '17 at 13:39
  • It prints out the absolute path to get to the directory containing the class. – Davide3i Dec 22 '17 at 13:43
  • Post the complete stack trace – James_D Dec 22 '17 at 23:44
  • Ok, I'll do it next morning. I've found out I'm having issues linking every kind of resource file. I've tried lot of methods, like class.getResource() or getResourceAsStream(), with no avail. See you tomorrow and thank you. – Davide3i Dec 23 '17 at 00:28
  • Question updated with the stack trace log. – Davide3i Dec 23 '17 at 09:42

2 Answers2

4

After some time I found a solution to this nasty issue and I'll try my best to explain it here.

First of all I've set up my Project Structure (File > Project Structure...) in a proper way, defining both the Source and Resource folders, so that IntelliJ Idea would be able to find them. Moreover I've checked that my resources files had been properly put in the "build" and "out" folders (they're used by Gradle and IntelliJ during the building process): I've also achieved it by setting my "File|Settings|Compiler" settings as nicely pinpointed in many Stack Overflow answers.
At last I've used this structure while calling for my resources files:

FXMLLoader loader = new FXMLLoader();
System.out.println(MainView.class.getResource("/fxml/MainView.fxml"));
loader.setLocation(MainView.class.getResource("/fxml/MainView.fxml"));

I want to pinpoint that my fxml folder has the Resources one as root folder.

Davide3i
  • 1,035
  • 3
  • 15
  • 35
0

The problem may be that IntelliJ does not know that it needs to copy the .fxml file into the output directory. You may need to add something like ";?.fxml;?.css" to your File|Settings|Compiler settings to tell it to copy the files during a build. See How to convert a normal java project in intellij into a JavaFx project for more information.

Some Guy
  • 405
  • 8
  • 15
  • Hi! At last, a couple of months ago, I've solved my problem fiddling with the IntelliJ Idea project settings and looking around Stack Overflow for the proper way to call up the path to a file. – Davide3i Apr 12 '18 at 18:11
  • 1
    Hi, @Davide3i. Congrats for figuring out the problem. I encourage you to update this bug with the solutions that you found so that anybody else who has the same problem can find the solution here. (It can be [Very Frustrating](https://xkcd.com/979/) when searching for a solution to find a statement that a question has been answered, with no answer given.) – Some Guy Apr 26 '18 at 00:37