Search code examples
javafxmlclassnotfoundexception

classnotfound exception but everything looks correct. can someone tell me if my start function is wrong?


This is my start function in my main class:

public void start(Stage primary) {
    stage = primary;

    FXMLLoader loader = new FXMLLoader();
    loader.setLocation(getClass().getResource("UserInterface.fxml"));
    try {
        Parent root = (Parent) loader.load();
        Scene scene = new Scene(root);
        primary.setScene(scene);
    }catch( Exception e) {
        e.printStackTrace();
    }
}

the error:

javafx.fxml.LoadException: 
/C:/Users/micha/eclipse-workspace/woolard2/bin/masterfile/UserInterface.fxml

at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
at javafx.fxml.FXMLLoader.importClass(FXMLLoader.java:2848)
at javafx.fxml.FXMLLoader.processImport(FXMLLoader.java:2692)
at javafx.fxml.FXMLLoader.processProcessingInstruction(FXMLLoader.java:2661)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2517)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2409)
at masterfile.Main.start(Main.java:42)
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)
at java.lang.Thread.run(Thread.java:748)

Caused by: java.lang.ClassNotFoundException
at javafx.fxml.FXMLLoader.loadType(FXMLLoader.java:2899)
at javafx.fxml.FXMLLoader.importClass(FXMLLoader.java:2846)
... 15 more

When I comment out everything in the "try" everything works and compiles, yet when I try setting the load to root it breaks.

So it is finding the file successfully, yet won't load because it can't find the class, but it's in the same folder as class and everything.... can someone please help me

edit- UserInterface.fxml -

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

<?import javafx.scene.Scene?>

<Scene xmlns:fx="http://javafx.com/fxml/1" fx:controller="model.UserController.java">

</Scene>

Solution

  • In your FXML you have defined the fx:controller property as model.UserController.java. However your class is not UserController.java, that is the name of the source file, the classname is UserController. Modify your FXML to use fx:controller="model.UserController".