0

Here's my structure: stupid stack overflow won't let me post this image

In my main class I'm calling this:

public class Main extends Application {
   @Override
   public void start(Stage primaryStage) throws Exception {
      Parent root = FXMLLoader.load(getClass().getResource("Form.fxml"));
      primaryStage.setTitle("Hello World");
      primaryStage.setScene(new Scene(root));
      primaryStage.setResizable(false);
      primaryStage.show();
   }
}

But this gets a huge error in return:

Exception in Application start method
java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
    at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$155(LauncherImpl.java:182)
    at java.lang.Thread.run(Thread.java:745)
Caused by: javafx.fxml.LoadException: 
/C:/Users/myname/Dropbox/IntellijWorkspace/Somefoodplus/target/classes/Form.fxml

    at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2579)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
    at Main.start(Main.java:11)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
    ... 1 more
Caused by: java.lang.NullPointerException
    at SomeFoodPlus.FXML.Controller.initialize(Controller.java:22)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548)
    ... 17 more
Exception running application Main

I really don't know what I could be doing wrong, since my FXML file is inside of my resources folder, and my Main.class is inside of my java package. I'm pretty sure it has to do with the project being a maven project, but I'm not sure what it could be. Please help me figure this out, I've looked online for solutions already and no one seems to do maven+fxml projects.

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
Jeff smith
  • 147
  • 7

3 Answers3

2

Try "/Form.fxml". See the Java Class API.

Jonathan Rosenne
  • 2,159
  • 17
  • 27
0

What Jonathan Rosenne says is essentially true, if you try getResource("/Form.fxml"), it should work. This is because Class.getResource uses a package-relative path, not an absolute path. The code that you have right now would work if Form.fxml was in located at resources/<packages>/Form.fxml, for example.

Another option is to use getClass().getClassLoader().getResource, which always assumes an absolute path.

There's already a StackOverflow thread about the differences between these two methods.

Spaceman1701
  • 413
  • 5
  • 13
  • Didn't work unfortunately, I still get the same error. I am using Java 8 though, and as soon as I add support to my pom.xml, I start getting the error. Any ideas? This is the code I add to my pom.xml: https://pastebin.com/raw/nEcXEfWD – Jeff smith Oct 24 '17 at 19:23
  • That's very strange. Are you saying that it works normally when you remove the maven build plugin from your POM? Also, have you used the debugger to confirm that `getResource` is returning null? – Spaceman1701 Oct 24 '17 at 20:13
  • Yeah it's for sure returning null :( https://i.imgur.com/uyRskuH.png And yeah, if I change the language to anything less than 8, it works fine. As soon as I add Java 8, I have to add that plugin to maven to work with Java 8. Maybe there's another solution I don't know of? – Jeff smith Oct 25 '17 at 04:47
  • Wow, when I go back to using getClass().getResource("/form.fxml"), the resource doesn't return null, but I still get an error, but it's slightly different: https://pastebin.com/raw/b3FKnpde What the heck? https://i.imgur.com/kxX4Hi9.png – Jeff smith Oct 25 '17 at 04:50
  • 1
    Well, I guess it was a combination of a few errors, but this ended up fixing it: https://stackoverflow.com/questions/23132302/invocationtargetexception-when-running-a-javafx-program – Jeff smith Oct 25 '17 at 04:57
0

Try below:

URL formUrl = Main.class.getClassLoader().getResource("Form.fxml");
Parent root = FXMLLoader.load(formUrl );
Pingolin
  • 3,161
  • 6
  • 25
  • 40
Kent Bong
  • 1
  • 1