Question

[Solved] “javafx.fxml.LoadException” when loading fxml

The error is being produced at showAdminLoginScene() method,
However if I modify this method and I put another fxml file from the same package to load, it loads just fine : here’s my Main :

package application;

import java.io.IOException;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;

public class Main extends Application {
  private Stage primaryStage;
  private static BorderPane mainLayout;
  @Override
public void start(Stage primaryStage) throws IOException {
    this.primaryStage= primaryStage;
    this.primaryStage.setTitle("LAU LABS");
    showMainView();
}
private void showMainView() throws IOException{
    FXMLLoader loader=new FXMLLoader();
    loader.setLocation(Main.class.getResource("MainView.fxml"));

    mainLayout=loader.load();


    Scene scene=new Scene(mainLayout);
    primaryStage.setScene(scene);
    primaryStage.show();
}

public static void showAdminLoginScene() {
    FXMLLoader loader=new FXMLLoader();
    loader.setLocation(Main.class.getResource("AdminLogin.fxml"));
    BorderPane adminLogin;
    try {
        adminLogin = loader.load();
        mainLayout.setCenter(adminLogin);
        } catch (IOException e) {
        e.printStackTrace();
    }

}
public static void showAdminLoggedIn() {
    FXMLLoader loader=new FXMLLoader();
    loader.setLocation(Main.class.getResource("AdminLoggedIn.fxml"));
    AnchorPane adminLoggedIn;
    try {
        adminLoggedIn = loader.load();
        mainLayout.setCenter(adminLoggedIn);
    } catch (IOException e) {
        System.out.println("the error is in Main, ShowAdminLoggedIn");
    }

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

And here’s my controller class :

package application;

import java.awt.Label;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.io.IOException;

import javafx.fxml.FXML;


public class AdminLoginController {
   @FXML private TextField txtUsername;
   @FXML private TextField txtPassword;
   @FXML private Label lblStatus;
   private Main main;


public void Login(ActionEvent event) {
   if(txtUsername.getText().equals("user")&& 
         txtPassword.getText().equals("pass")){
    main.showAdminLoggedIn();
}
else{
    lblStatus.setText("Login Failed!");
}
}
}

Here’s my fxml :

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

<?import com.gluonhq.charm.glisten.control.TextField?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.PasswordField?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.text.Text?>


<BorderPane xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.AdminLoginController">
   <center>
      <AnchorPane prefHeight="300.0" prefWidth="300.0">
         <children>
            <TextField fx:id="txtUsername" layoutX="56.0" layoutY="79.0" prefHeight="26.0" prefWidth="189.0" />
            <PasswordField fx:id="txtPassword" layoutX="56.0" layoutY="150.0" prefHeight="26.0" prefWidth="189.0" />
            <Button layoutX="56.0" layoutY="227.0" mnemonicParsing="false" onAction="#Login" prefHeight="32.0" prefWidth="189.0" style="-fx-background-color: #004c3c;" text="Login" />
            <Text fill="#004c3c" layoutX="56.0" layoutY="75.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Username" wrappingWidth="60.98307228088379" />
            <Text fill="#004c3c" layoutX="56.0" layoutY="146.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Password" />
            <Button layoutX="1.0" minWidth="52.0" mnemonicParsing="false" prefHeight="39.0" prefWidth="52.0" style="-fx-background-color: Transparent;" />
            <ImageView fitHeight="35.0" fitWidth="200.0" layoutX="10.0" layoutY="14.0" pickOnBounds="true" preserveRatio="true">
               <image>
                  <Image url="@BackButton.png" />
               </image>
            </ImageView>
            <Label fx:id="lblStatus" layoutX="56.0" layoutY="202.0" prefHeight="16.0" prefWidth="189.0" style="-fx-text-fill: Red;" text="Status: " />
         </children>
      </AnchorPane>
   </center>
</BorderPane>

They’re all in the same package named “application” (the fxml, controller and main)

That’s the error :

javafx.fxml.LoadException: 
/C:/Users/Owner/Documents/UNI/programming/workspace/Lau/bin/application/AdminLogin.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 application.Main.showAdminLoginScene(Main.java:39)
    at application.MainViewController.goAdminLogin(MainViewController.java:8)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at sun.reflect.misc.Trampoline.invoke(Unknown Source)
    at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at sun.reflect.misc.MethodUtil.invoke(Unknown Source)
    at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1771)
    at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1657)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
    at javafx.event.Event.fireEvent(Event.java:198)
    at javafx.scene.Node.fireEvent(Node.java:8413)
    at javafx.scene.control.Button.fire(Button.java:185)
    at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182)
    at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:96)
    at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89)
    at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
    at javafx.event.Event.fireEvent(Event.java:198)
    at javafx.scene.Scene$MouseHandler.process(Scene.java:3757)
    at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
    at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
    at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:381)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$354(GlassViewEventHandler.java:417)
    at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:416)
    at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
    at com.sun.glass.ui.View.notifyMouse(View.java:937)
    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: com.gluonhq.charm.glisten.control.TextField
    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.loadTypeForPackage(FXMLLoader.java:2916)
    at javafx.fxml.FXMLLoader.loadType(FXMLLoader.java:2905)
    at javafx.fxml.FXMLLoader.importClass(FXMLLoader.java:2846)
    ... 65 more

Solution #1:

You are using an incorrect TextField (I guess it is not intentional) in your FXML:

<?import com.gluonhq.charm.glisten.control.TextField?>

which is causing:

Caused by: java.lang.ClassNotFoundException: com.gluonhq.charm.glisten.control.TextField

You can change the import as …

<?import javafx.scene.control.TextField?>

to use the built-in TextField.

Respondent: DVarga

Solution #2:

You’re missing Gluon’s Charm library, you should add it in your build automation tool:

For maven project:

<dependencies>
...
<dependency>
    <groupId>com.gluonhq</groupId>
    <artifactId>charm-glisten</artifactId>
    <version>4.4.1</version> // Change version as needed 
</dependency>
...
</dependencies>
<repositories>
       <repository>
         <id>nexus-com-gluonhq</id>
         <name>Nexus repo</name>
         <url>http://nexus.gluonhq.com/nexus/content/repositories/releases</url>
       </repository>
</repositories>

for Gradle:

.

..
repositories {
    ....
    maven {
        url 'http://nexus.gluonhq.com/nexus/content/repositories/releases'
    }
   ...
}

dependencies {
    compile 'com.gluonhq:charm-glisten:4.4.1'
    ...
}

By the way, in your controller class use javafx.scene.control instead of java.awt as you’re using JavaFX project

Respondent: O.Badr

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Most Popular

To Top
India and Pakistan’s steroid-soaked rhetoric over Kashmir will come back to haunt them both clenbuterol australia bossier man pleads guilty for leadership role in anabolic steriod distribution conspiracy