2

I have a standard (Non JavaFX) Maven Java Project in Netbeans. The first thing it is supposed to do is load a .fxml file, but I can't seem to find it using getClass().getResource().

This is unusual, because this project is imported from a standard Java project, which runs just fine without error.

Here is the main method:

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

@Override
public void start(Stage stage) throws Exception {
    Main_PanelController controller = new Main_PanelController(this);
    FXMLLoader loader = new FXMLLoader(getClass().getResource("Main_Panel.fxml"));
    loader.setController(controller);
    URL url = getClass().getResource("Main_Panel.fxml");
    System.out.println("URL: " + url);
    System.exit(0);
    Parent root = loader.load();
    Scene scene = new Scene(root);
    stage.setScene(scene);
    stage.getIcons().add(new Image(new FileInputStream(Paths.get("Pictures", "SBU Emblem.png").toFile())));
    stage.setAlwaysOnTop(true);
    stage.setTitle("Simple Knisley Citation Tool α 0.5");
    stage.setResizable(false);
    stage.show();
}

The URL always comes back as null.

& The File Hierarchy:

Hierarchy

I have the System.out.println as a test method to see if the URL object comes back as null or not. According to everything I've read about getClass().getResource() it should get any object in the same package as the calling class, which Main_Panel.fxml is.

Here is the Jar file structure from the target folder after Clean & Build has run using the command jar tf Simple-Knisley-0.5-Alpha-jackofall.jar

META-INF/
META-INF/MANIFEST.MF
com/
com/protonmail/
com/protonmail/sarahszabo/
com/protonmail/sarahszabo/simpleknisley/
com/protonmail/sarahszabo/simpleknisley/core/
com/protonmail/sarahszabo/simpleknisley/core/CitationDiskManager.class
com/protonmail/sarahszabo/simpleknisley/core/Main_PanelController.class
com/protonmail/sarahszabo/simpleknisley/core/CitationGenerator$GeneratorType.class
com/protonmail/sarahszabo/simpleknisley/core/CitationGenerator.class
com/protonmail/sarahszabo/simpleknisley/core/Main.class
.netbeans_automatic_build
META-INF/maven/
META-INF/maven/com.protonmail.sarahszabo.simpleknisley/
META-INF/maven/com.protonmail.sarahszabo.simpleknisley/Simple-Knisley/
META-INF/maven/com.protonmail.sarahszabo.simpleknisley/Simple-Knisley/pom.xml
META-INF/maven/com.protonmail.sarahszabo.simpleknisley/Simple-Knisley/pom.properties

I'm using Java 8_171 on Netbeans 8.2 on Kubuntu 18.04 Bionic Beaver

fabian
  • 80,457
  • 12
  • 86
  • 114
Sarah Szabo
  • 10,345
  • 9
  • 37
  • 60
  • Have a look at this post : https://stackoverflow.com/questions/3803326/this-getclass-getclassloader-getresource-and-nullpointerexception , in my opinion this has to do about Maven, if so I am not familiar with but you may try to use a full path like : `getClass().getResource("/com/protonmail/sarahszabo/simpleknisley/core/Main_Panel.fxml")` – JKostikiadis Jun 18 '18 at 03:09
  • There are a lot of people in the post in my previous commend who tweak the maven build path configuration system or the setting in general to fix the problem. So read some of their answers if my previous suggestion isn't working. – JKostikiadis Jun 18 '18 at 03:11
  • I not see FXML file in this jar. Une resource folder for fxml files. – mr mcwolf Jun 18 '18 at 04:37
  • Have you tried adding a "/" in front of your resource (`getClass().getResource("/Main_Panel.fxml");`)? That is a trap I always fall for when using that method. – geisterfurz007 Jun 18 '18 at 05:05
  • I'm not sure if this is the kind of project, but some project types require you to put the resources in a seperate "resource tree", i.e. there may be a seperate "Other sources" item in your project you need to move the fxmls to (and reproduce the correct package structure). – fabian Jun 18 '18 at 12:00
  • @geisterfurz007 Yep tried that, no luck. I saw that solution from my research on `getResource()` – Sarah Szabo Jun 18 '18 at 15:48

1 Answers1

3

There are a few issues going on here.

  1. Maven expects (by default) resources to be in the src/main/resources directory. It will only copy java files from src/main/java by default. So move your fxml file to src/main/resources/com/protonmail/sarahszabo/simpleknisley/core/Main_Panel.fxml

  2. getClass().getResource() expects you to give the full path to the resource that you want to load. So in your case: getClass().getResource("/com/protonmail/sarahszabo/simpleknisley/core/Main_Panel.fxml")

Using getClass().getClassLoader().getResource() allows you to specify the location of the resource you want to load as a relative path to the class you are calling it in:

getClass().getClassLoader().getResource("Main_Panel.fxml")
John Farrelly
  • 7,289
  • 9
  • 42
  • 52
  • Now when you say `/src/main` what packages should I create in Netbeans? Should I make the package `src/main/resources/com/protonmail/sarahszabo/simpleknisley/core/Main_Panel.fxml` or is the `src` essentially the same as the Source Code folder? – Sarah Szabo Jun 18 '18 at 15:53
  • Yes, the full path you have above is what you should create. You might be better creating it on the command line rather than netbeans, incase it makes some assumptions you're not expecting. – John Farrelly Jun 18 '18 at 16:26
  • I Made the `src/main/resources/com/protonmail/sarahszabo/simpleknisley/core/` folder and swiched the `getResources ` call to `getClass().getClassLoader().getResource("Main_Panel.fxml")` as well as specifying the full path, but this appeared to be ineffective. – Sarah Szabo Jun 18 '18 at 16:41
  • I also didn't see the `/src` folder or any of its children in the `jar tf` structure after building it again. – Sarah Szabo Jun 18 '18 at 16:47
  • Did you move the fxml file to the new folder? – John Farrelly Jun 18 '18 at 17:34
  • Yes, I moved it to `src/main/resources/com/protonmail/sarahszabo/simpleknisley/core`. – Sarah Szabo Jun 18 '18 at 17:42
  • What do you see when you run `find src/main -type f` from the root directory of your project? – John Farrelly Jun 18 '18 at 22:37