0

Hi i am using javaFx and i am new to it. i need help in it. while running the main application i am getting error of javafx.fxml.LoadException:

Please Help.

Code: Main Code

package mainApp;


import controller.LoginController;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;

public class Main extends Application {
    @Override
    public void start(Stage primaryStage) {
        try {
            //Create a loader for the UI components
            FXMLLoader loader = new FXMLLoader();
                    loader.setLocation(Main.class.getResource("/view/loginFx.fxml"));
            //Inflate the view using the loader
            AnchorPane root = (AnchorPane) loader.load();
            //Set window title
            primaryStage.setTitle("Welcome");
            //Create a scene with the inflated view
            Scene scene = new Scene(root);
            //Set the scene to the stage
            primaryStage.setScene(scene);
            //Get the controller instance from the loader
            LoginController controller = loader.getController();
            //Set the parent stage in the controller
            controller.setDialogStage(primaryStage);
            //Show the view
            primaryStage.show();
        } catch(Exception e) {
            //System.out.println("Error occured while inflating view: " + e);
            e.printStackTrace();
        }
    }


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

fxml file

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

<?import java.net.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.AnchorPane?>

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1" fx:controller="login.controller.LoginController">
    <children>
        <Label layoutX="113.0" layoutY="102.0" text="UserName" />
        <Label layoutX="116.0" layoutY="173.0" text="Password" />
        <TextField fx:id="UserName" layoutX="200.0" layoutY="98.0" />
        <Button layoutX="242.0" layoutY="251.0" mnemonicParsing="false" onAction="#loginbutton" text="Login" />
        <Text fx:id="username" layoutX="200.0" layoutY="400.0" />
      <PasswordField layoutX="200.0" layoutY="169.0" />
      <Label layoutX="54.0" layoutY="45.0" prefHeight="17.0" prefWidth="130.0" text="Welcome" />
   </children>

</AnchorPane>

Controller file:

package controller;


//import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.TextField;
//import javafx.scene.text.Text;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;

import model.Login;
import dao.UserDao;

public class LoginController {
    //This is the parent stage
        private Stage dialogStage;

        //This is the Text box element in the view for name of bank
        @FXML
        private TextField username;
        //Method to set the parent stage of the current view
        public void setDialogStage(Stage dialogStage) {
            this.dialogStage = dialogStage;
        }
        public void loginbutton() {
            //Extract the data from the view elements
            String username = this.username.getText();
        //  String address = this.address.getText();
            //Validate the data
            if(username == null || username.trim().equals("")) {
                return;
            }

        Login login = new Login();
        login.setName(username);
        UserDao u = new UserDao();
        u.create(login);
        close();
        }

        private void close() {
            dialogStage.fireEvent(new WindowEvent(dialogStage,WindowEvent.WINDOW_CLOSE_REQUEST));
        }

    }

Please tell me what is the issue i am getting following error: javafx.fxml.LoadException: /C:/Users/Neel-Megha/workspace/User/bin/view/loginFx.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.load(FXMLLoader.java:2409)
at mainApp.Main.start(Main.java:19)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$163(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$176(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$174(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at 

com.sun.javafx.application.PlatformImpl.lambda$runLater$175(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$149(WinApplication.java:191)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.ClassNotFoundException: login.controller.LoginController
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:920)
    ... 17 more

1 Answers1

0

The stack trace tells you the problem:

Caused by: java.lang.ClassNotFoundException: login.controller.LoginController

You declared your controller in the FXML file as fx:controller="login.controller.LoginController", so the FXMLLoader is looking for a LoginController class in the login.controller package. However, your LoginController class declares it is in the controller package:

package controller ;

// ...

public class LoginController { ... }

So you should change the FXML to read fx:controller="controller.LoginController".

(There may be other errors in your code: this is the one causing the error you posted.)


As an aside, you don't need all the code that wires your Stage instance into the controller. Just do

private void close() {
    username.getScene().getWindow().hide();
}

and now you can get rid of the dialogStage and the setDialogStage() method from LoginController, and you can remove the lines

LoginController controller = loader.getController();
controller.setDialogStage(primaryStage);

from Main.

(There are certainly times when you need access to the controller, but all this unnecessary wiring of the stage, which is accessible from any node in the UI, drives me crazy.)

James_D
  • 201,275
  • 16
  • 291
  • 322