Problem loading an .fxml file

0

I'm starting to work with JavaFX but when I try to show my .fxml file it sends me the following error

javafx.fxml.LoadException: 
/C:/Users/Javier/eclipse-workspace/Hola/bin/application/Form.fxml:11

    at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
    at javafx.fxml.FXMLLoader.access$700(FXMLLoader.java:103)
    at javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:922)
    at javafx.fxml.FXMLLoader$InstanceDeclarationElement.processAttribute(FXMLLoader.java:971)
    at javafx.fxml.FXMLLoader$Element.processStartElement(FXMLLoader.java:220)
    at javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:744)
    at javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2707)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2527)
    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 application.Main.start(Main.java:15)
    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(Unknown Source)
Caused by: java.lang.ClassNotFoundException: MyController
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:920)
    ... 22 more

This is my structure

  

Main Class

public class Main extends Application {
@Override
public void start(Stage primaryStage) {
    try {
        Parent root = FXMLLoader.load(getClass().getResource("Form.fxml"));
        Scene esena = new Scene(root);

        primaryStage.setScene(esena);
        primaryStage.show();

    } catch(Exception e) {
        e.printStackTrace();
    }
}

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

}

  

Controller

public class MyController implements Initializable{

@Override
public void initialize(URL location, ResourceBundle resources) {
    // TODO Auto-generated method stub

}

}

I hope and you can help me. Thanks

    
asked by Javier fr 11.07.2017 в 23:40
source

2 answers

0

I always start them that way

    FXMLLoader f = new FXMLLoader(getClass().getResource("/view/NuevoDialog.fxml"));
    f.setController(this);
    Parent root = f.load();
    Scene sc = new Scene(root);
    stage.setScene(sc);
    stage.sizeToScene();
    stage.show();
    
answered by 22.07.2017 в 09:40
0
  

Possible problems:

1. The fxml file is not correctly linked to its driver.

It happens sometimes when we make changes in the structure (if it worked before). If the file fxml to load is indicated to the class that is loading it with the line:

Parent root = FXMLLoader.load(getClass().getResource("Form.fxml"));

Inside this file is indicated in turn where the corresponding controller is, this you will have on line 11:

javafx.fxml.LoadException: 
/C:/Users/Javier/eclipse-workspace/Hola/bin/application/Form.fxml:11

this line inside the container of your window, generally an AnchorPane, must have the driver route correctly indicated, with the package and name of the class (look for them in the controller class):

fx:controller="application.MyController">
    
answered by 02.08.2018 в 20:08