0

I have a gui and I expect that given correct login credentials, a new window should open (The employeeFXML window) when the login button is pressed, however, that is not happening. I have taken a look at How do I determine the correct path for FXML files, CSS files, Images, and other resources needed by my JavaFX Application? but it does not solve my question. Below is my code for my main controller:

    @FXML
public void Login(ActionEvent event) throws Exception {

    //method here to check if correct login info input
    try{

              FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("employeeFXML.fxml"));
            Parent root1 = (Parent) fxmlLoader.load();
            Stage stage = new Stage();

            stage.setTitle("ABC");
            stage.setScene(new Scene(root1));
            stage.show();

    }
    catch (Exception exception){

    }
}

Below is the code for the login screen FXML:

    <AnchorPane id="AnchorPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="loginapp.Controller">
   <children>
      <Label layoutX="30.0" layoutY="14.0" text="Database status " />
      <TextField fx:id="username" layoutX="30.0" layoutY="125.0" />
      <Label layoutX="30.0" layoutY="99.0" text="Username" />
      <Label layoutX="30.0" layoutY="174.0" text="Password" />
      <PasswordField fx:id="password" layoutX="30.0" layoutY="200.0" />
      <ComboBox fx:id="combobox" layoutX="30.0" layoutY="268.0" prefWidth="150.0" promptText="Manager/Employee" />
      <Button fx:id="loginButton" layoutX="61.0" layoutY="338.0" mnemonicParsing="false" onAction="#Login" text="Login" />
      <Label fx:id="dbStatus" layoutX="186.0" layoutY="14.0" text="Label" />
      <Label fx:id="loginStatus" layoutX="146.0" layoutY="343.0" prefHeight="17.0" prefWidth="79.0" text="text" />
   </children>
</AnchorPane>

Below is the code for the start method:

public class Login extends Application{

Controller controller = new Controller();


public void start (Stage stage) throws Exception{
    Parent root = (Parent)FXMLLoader.load(getClass().getResource("login.fxml"));//was /loginapp/login.fxml
    Scene scene = new Scene(root);
    stage.setScene(scene);
    stage.setTitle("Employee management system");
    stage.show(); //showing the window
}

public static void main (String [] args){
    launch(args); //this line of code will run the application

}

}

So my application starts on that screen and when correct credentials are given and the login button is pressed it should go to the next screen but this is not the case. Below is a pic of the starting screen of the GUI: GUI

Below is the error message I get:

java.lang.IllegalStateException: Location is not set.
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2434)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2409)
at loginapp.Controller.Login(Controller.java:70)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:72)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:276)
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1769)
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:8411)
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:394)
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$2(GlassViewEventHandler.java:432)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:431)
at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
at com.sun.glass.ui.View.notifyMouse(View.java:937)
  • You're swallowing the exception, meaning you prevent the program from telling you what went wrong. At the very least you should do `exception.printStackTrace()`. However you handle the exception, please post the stack trace in your question (as text, formatted as code). – Slaw Dec 20 '20 at 03:37
  • @Slaw your right, I posted the stacktrace – eddy_dimolokish123 Dec 20 '20 at 05:12
  • That error means `getResource("login.fxml")` is returning `null`, which in turn means there is no resource with that name (at least not relative to the `Login` class). The duplicate should explain how to determine the correct path to your FXML file. – Slaw Dec 20 '20 at 06:05
  • @Slaw that answer does not work. I am already passing my FXML file as a url using getClass().getResource() – eddy_dimolokish123 Dec 20 '20 at 17:00
  • But obviously not using the right path to get the URL, which is what the duplicate explains how to do. It's good that you're using `getResource(...)`. The problem is the argument you pass to `getResource(...)`. – Slaw Dec 20 '20 at 18:58
  • *Or the problem is that the resource is not being included at run-time for some reason (which the duplicate also provides some debugging tips for) – Slaw Dec 20 '20 at 23:21
  • @Slaw I see. That might be the case because im assuming the argument thats supposed to be passed into getResource should be the FXML file that I want to open upon the click of the button (The EmployeeFXML), right? – eddy_dimolokish123 Dec 21 '20 at 00:19
  • Yes, it should be the path to the FXML file. Right now your code assumes the `employeeFXML.fxml` resource is in the same package as whatever class the `#Login(ActionEvent)` method is in, because you're using a relative path. If that's not the case then use an absolute path. Again, the duplicate goes into great detail about how to use `getResource`. – Slaw Dec 21 '20 at 02:09

0 Answers0