0

I don't know what's wrong with this code block. I've made many researches about it but didn't find anything that's helpful. I have tried everything out.

I hope someone could help me out with it. The code is in a file called Bank, while the fxml files are in the resources<bank file.

Here is Main.java:

package Bank;

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

import java.io.IOException;

import static javafx.fxml.FXMLLoader.load;

public class Main extends Application {

    private static Stage stg;

    @Override
    public void start(Stage primaryStage) throws IOException {
        stg = primaryStage;
        primaryStage.setResizable(false);
        FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("Login.fxml"));
        Scene scene = new Scene(fxmlLoader.load(), 400, 550);
        primaryStage.setTitle("بنك احمد واولاده");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public void changeScene(String fxml) throws IOException {
        Parent pane = FXMLLoader.load(getClass().getResource(fxml));
        stg.getScene().setRoot(pane);
    }
    public static void main(String[] args) {
        launch();
    }
}

Here is Login.java:

package Bank;

import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.event.ActionEvent;
import java.io.IOException;

public class Login {
    public Login(){}

    @FXML
    private Button loginButton;
    @FXML
    private Label wrongLogin;
    @FXML
    private TextField username;
    @FXML
    private PasswordField password;


    public void userLogin(ActionEvent event) throws IOException {
        checkLogin();
    }

    private void checkLogin() throws IOException {
        Main m = new Main();
        if(username.getText().toString().equals("1") && password.getText().toString().equals("1")) {
            wrongLogin.setText("Success!");
            m.changeScene("Dashboard.fxml");
        }

        else if(username.getText().isEmpty() && password.getText().isEmpty()) {
            wrongLogin.setText("Please enter your data.");
        }

        else {
            wrongLogin.setText("Wrong username or password!");
        }
    }
}

Here is Login.fxml:

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

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.PasswordField?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.text.Font?>

<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="537.0" prefWidth="411.0" xmlns="http://javafx.com/javafx/19" xmlns:fx="http://javafx.com/fxml/1" fx:controller="Bank.Login">
   <center>
      <AnchorPane prefHeight="507.0" prefWidth="411.0" style="-fx-border-color: d8d8d8; -fx-background-color: dff2ff;" BorderPane.alignment="CENTER">
         <children>
            <Label layoutX="79.0" layoutY="245.0" text="Username">
               <font>
                  <Font name="System Bold" size="15.0" />
               </font>
            </Label>
            <Label layoutX="81.0" layoutY="294.0" text="Password">
               <font>
                  <Font name="System Bold" size="15.0" />
               </font>
            </Label>
            <TextField fx:id="username" layoutX="175.0" layoutY="243.0" promptText="Username" />
            <PasswordField fx:id="password" layoutX="175.0" layoutY="292.0" promptText="Password" />
            <Button fx:id="loginButton" alignment="CENTER" ellipsisString="" layoutX="169.0" layoutY="361.0" mnemonicParsing="false" onAction="#userLogin" prefHeight="34.0" prefWidth="73.0" style="-fx-background-color: d2d2d2;" text="Login">
               <font>
                  <Font name="Cambria" size="15.0" />
               </font>
            </Button>
            <ImageView fitHeight="124.0" fitWidth="104.0" layoutX="154.0" layoutY="44.0" pickOnBounds="true" preserveRatio="true">
               <image>
                  <Image url="@%5BCITYPNG.COM%5DFREE%20Round%20Blue%20Bank%20Icon%20PNG%20-%20512x512.png" />
               </image>
            </ImageView>
            <Label layoutX="133.0" layoutY="158.0" text="AURAK BANK">
               <font>
                  <Font name="Candara Light" size="25.0" />
               </font>
            </Label>
            <Label layoutX="150.0" layoutY="517.0" lineSpacing="1.0" text="All rights reserved to meow" textOverrun="WORD_ELLIPSIS">
               <font>
                  <Font name="System Italic" size="9.0" />
               </font>
            </Label>
            <Label fx:id="wrongLogin" alignment="CENTER" contentDisplay="CENTER" ellipsisString="%key.unspecified" layoutX="192.0" layoutY="329.0" prefHeight="22.0" prefWidth="5.0" textAlignment="CENTER" textFill="#a40000">
               <font>
                  <Font name="System Bold" size="15.0" />
               </font>
            </Label>
         </children>
      </AnchorPane>
   </center>
</BorderPane>

I'm sure the error is in this part

Parent pane = FXMLLoader.load(getClass().getResource(fxml));

The error:

C:\Users\ahmed\.jdks\openjdk-19.0.1\bin\java.exe --module-path D:\ahmed\Downloads\bank\javafx-sdk-19\lib --add-modules javafx.controls,javafx.fxml --add-exports javafx.graphics/com.sun.javafx.sg.prism=ALL-UNNAMED "-javaagent:D:\Applications\IntelliJ IDEA 2022.2.4\lib\idea_rt.jar=61272:D:\Applications\IntelliJ IDEA 2022.2.4\bin" -Dfile.encoding=UTF-8 -Dsun.stdout.encoding=UTF-8 -Dsun.stderr.encoding=UTF-8 -classpath D:\ahmed\Downloads\bank\bank\target\classes;C:\Users\ahmed\.m2\repository\org\openjfx\javafx-controls\19-ea+7\javafx-controls-19-ea+7.jar;C:\Users\ahmed\.m2\repository\org\openjfx\javafx-controls\19-ea+7\javafx-controls-19-ea+7-win.jar;C:\Users\ahmed\.m2\repository\org\openjfx\javafx-graphics\19-ea+7\javafx-graphics-19-ea+7.jar;C:\Users\ahmed\.m2\repository\org\openjfx\javafx-graphics\19-ea+7\javafx-graphics-19-ea+7-win.jar;C:\Users\ahmed\.m2\repository\org\openjfx\javafx-base\19-ea+7\javafx-base-19-ea+7.jar;C:\Users\ahmed\.m2\repository\org\openjfx\javafx-base\19-ea+7\javafx-base-19-ea+7-win.jar;C:\Users\ahmed\.m2\repository\org\openjfx\javafx-fxml\19-ea+7\javafx-fxml-19-ea+7.jar;C:\Users\ahmed\.m2\repository\org\openjfx\javafx-fxml\19-ea+7\javafx-fxml-19-ea+7-win.jar;D:\ahmed\Downloads\bank\javafx-sdk-19\lib\javafx-swt.jar;D:\ahmed\Downloads\bank\javafx-sdk-19\lib\javafx.web.jar;D:\ahmed\Downloads\bank\javafx-sdk-19\lib\javafx.base.jar;D:\ahmed\Downloads\bank\javafx-sdk-19\lib\javafx.fxml.jar;D:\ahmed\Downloads\bank\javafx-sdk-19\lib\javafx.media.jar;D:\ahmed\Downloads\bank\javafx-sdk-19\lib\javafx.swing.jar;D:\ahmed\Downloads\bank\javafx-sdk-19\lib\javafx.controls.jar;D:\ahmed\Downloads\bank\javafx-sdk-19\lib\javafx.graphics.jar Bank.Main
Exception in Application start method
java.lang.reflect.InvocationTargetException
    at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:119)
    at java.base/java.lang.reflect.Method.invoke(Method.java:578)
    at javafx.graphics@19/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:465)
    at javafx.graphics@19/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:364)
    at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104)
    at java.base/java.lang.reflect.Method.invoke(Method.java:578)
    at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1081)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at javafx.graphics@19/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:901)
    at javafx.graphics@19/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:196)
    at java.base/java.lang.Thread.run(Thread.java:1589)
Caused by: java.lang.IllegalStateException: Location is not set.
    at javafx.fxml@19/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2541)
    at javafx.fxml@19/javafx.fxml.FXMLLoader.load(FXMLLoader.java:2516)
    at Bank.Main.start(Main.java:20)
    at javafx.graphics@19/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:847)
    at javafx.graphics@19/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:484)
    at javafx.graphics@19/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:457)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
    at javafx.graphics@19/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:456)
    at javafx.graphics@19/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
    at javafx.graphics@19/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at javafx.graphics@19/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:184)
    ... 1 more
Exception running application Bank.Main

Process finished with exit code 1

I'd really appreciate your help, I looked into other solutions but none of them worked.

Ahmed
  • 1
  • That error means `getClass().getResource("Login.fxml")` is returning `null`, which means the FXML resource could not be found at `"Login.fxml"` (i.e., you're either using the wrong resource path or the resource is not being "deployed"). See the duplicate for details (including common debugging techniques for these kind of errors). – Slaw Nov 25 '22 at 21:21
  • unrelated: stick to java naming conventions (package names should be all lower-case) - and don't hard-code sizing constraints nor locations (which boils down to using another layout than AnchorPane) – kleopatra Nov 26 '22 at 00:05

0 Answers0