FXML: ClassNotFoundException

1.6k views Asked by At

I'm very new to JavaFX and I'm having problem using my custom class in FXML. The console keeps giving me this exception when trying to load main.fxml:

... 1 more
Caused by: java.lang.ClassNotFoundException: sample.View$BoardPane
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
...

I created BoardPane class as a subclass of FlowPane in package sample.View and referred to it in my FXML as the following:

<?import sample.View.BoardPane?>
...
<TitledPane expanded="true" collapsible="false" text="BoardPane" fx:id="centerTitledPane">
    <BoardPane fx:id="mechoBoardPane"/>
</TitledPane>
...

and the project structure looks like this:

  • resources
    • fxml
      • main.fxml
  • ...
  • src
    • ...
    • sample
      • ...
      • View
        • BoardPane

Can anyone please help me on this? I've been searching for some time and haven't found any explanation.

1

There are 1 answers

2
Roland On BEST ANSWER

The package name "View" must be lower case. In your project, as well as in the fxml.

For more details see the method loadType of FXMLLoader.class:

private Class<?> loadType(String name, boolean cache) throws ClassNotFoundException {
    int i = name.indexOf('.');
    int n = name.length();
    while (i != -1
        && i < n
        && Character.isLowerCase(name.charAt(i + 1))) {  // <<<<<<<<<
        i = name.indexOf('.', i + 1);
    }

    if (i == -1 || i == n) {
        throw new ClassNotFoundException();
    }

    String packageName = name.substring(0, i);
    String className = name.substring(i + 1);

    Class<?> type = loadTypeForPackage(packageName, className);

    if (cache) {
        classes.put(className, type);
    }

    return type;
}