0

I tried creating a JavaFX project in Eclipse and as long as I didn't change the default file location everything worked fine. This is the newly craeted project

I want instead to put the SceneControllers in a different package from Main, and .fxml files in another package.

I want it to become more like this Expected folders

But when i run the app i get this error `

java.lang.NullPointerException: Location is required.
    at javafx.fxml@19/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3324)
    at javafx.fxml@19/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3287)
    at javafx.fxml@19/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3255)
    at javafx.fxml@19/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3227)
    at javafx.fxml@19/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3203)
    at javafx.fxml@19/javafx.fxml.FXMLLoader.load(FXMLLoader.java:3196)
    at application.classes.Main.start(Main.java:14)
    at javafx.graphics@19/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:847)
    at javafx.graphics@19/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:484)
    at javafx.graphics@19/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:457)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
    at javafx.graphics@19/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:456)
    at javafx.graphics@19/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
    at javafx.graphics@19/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at javafx.graphics@19/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:184)
    at java.base/java.lang.Thread.run(Thread.java:833)

`

So i tought the issue was to specify the location of the "Sample.fxml" file, and at line 14 wrote: AnchorPane root = (AnchorPane)FXMLLoader.load(getClass().getResource("../Sample.fxml"));

And got this error: `

javafx.fxml.LoadException: 
/C:/Users/ultra/eclipse-workspace/Shopping%20Online/bin/application/guifiles/Sample.fxml:5

    at javafx.fxml@19/javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2707)
    at javafx.fxml@19/javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:933)
    at javafx.fxml@19/javafx.fxml.FXMLLoader$InstanceDeclarationElement.processAttribute(FXMLLoader.java:981)
    at javafx.fxml@19/javafx.fxml.FXMLLoader$Element.processStartElement(FXMLLoader.java:230)
    at javafx.fxml@19/javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:755)
    at javafx.fxml@19/javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2808)
    at javafx.fxml@19/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2634)
    at javafx.fxml@19/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548)
    at javafx.fxml@19/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3331)
    at javafx.fxml@19/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3287)
    at javafx.fxml@19/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3255)
    at javafx.fxml@19/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3227)
    at javafx.fxml@19/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3203)
    at javafx.fxml@19/javafx.fxml.FXMLLoader.load(FXMLLoader.java:3196)
    at application.classes.Main.start(Main.java:14)
    at javafx.graphics@19/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:847)
    at javafx.graphics@19/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:484)
    at javafx.graphics@19/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:457)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
    at javafx.graphics@19/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:456)
    at javafx.graphics@19/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
    at javafx.graphics@19/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at javafx.graphics@19/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:184)
    at java.base/java.lang.Thread.run(Thread.java:833)
Caused by: java.lang.ClassNotFoundException: application.SampleController
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
    at javafx.fxml@19/javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:931)
    ... 22 more

`

Nathh.png
  • 3
  • 2
  • looks like you have some syntax error in your `Sample.fxml` file – Rustam Dec 05 '22 at 18:18
  • Isn't the error message enough: "ClassNotFoundException: application.SampleController"? But you should also note that you should not use `..` in a resource path; it will fail when the application is deployed. See https://stackoverflow.com/questions/61531317/how-do-i-determine-the-correct-path-for-fxml-files-css-files-images-and-other – James_D Dec 05 '22 at 18:20
  • Just some constructive feedback, but providing a project download drastically helps other reproduce the problem and can get answers quicker. https://en.wikipedia.org/wiki/Minimal_reproducible_example – tresf Dec 05 '22 at 18:26
  • Im kinda new to Java/JavaFX actually so im not really good at dealing with errors :'D Thanks for the feedback btw – Nathh.png Dec 05 '22 at 19:52
  • *"I'm not really good at dealing with errors."* Reading a [stack trace](https://stackoverflow.com/q/3988788/) is a fundamental Java skill. I really recommend being familiar with Java basics before attempting to use a moderately advanced library such as JavaFX. – James_D Dec 06 '22 at 12:52

1 Answers1

4

The new error is

ClassNotFoundException: application.SampleController

which occurs when you try to load the FXML, specifically on line 5 of Sample.fxml.

When you moved your controller class SampleController from the application package to the application.controllers package, you changed its fully-qualified class name. So you need to change the fx:controller attribute in the FXML file (on line 5) accordingly:

fx:controller="application.controllers.SampleController"

As an aside, note that the resource path you are using for the FXML file is not valid, as .. is not a valid resource name. It might work while you are still running the application from the file system, but will not work if you bundle it as a jar file. See How do I determine the correct path for FXML files, CSS files, Images, and other resources needed by my JavaFX Application?

James_D
  • 201,275
  • 16
  • 291
  • 322