3

I can not figure out for the life of me the problem with this code. I have done research on many similar questions here, addressing whether the directories were correct, possible wrong function calls etc.

I am hoping someone can help me out. Everything is in a file called login in an app called loginapp.

Here is Login.java

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package login;

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


public class Login extends Application   {

    @Override
    public void start(Stage stage) throws Exception {
        FXMLLoader loader = new FXMLLoader(getClass().getResource("Login.fxml"));
        Parent root = FXMLLoader.load(getClass().getClassLoader().getResource("Login.fxml"));
        Scene scene = new Scene(root);
        stage.setScene(scene);
        stage.setTitle("Fracken");
        stage.show();
    }
}

Here is Login.fxml

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

<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<AnchorPane id="AnchorPane" prefHeight="317.0" prefWidth="326.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="login.Login">
   <children>
      <TextField fx:id="txtUsername" layoutX="110.0" layoutY="45.0" promptText="Username" />
      <PasswordField fx:id="txtPassword" layoutX="110.0" layoutY="115.0" promptText="Password" />
      <Button fx:id="btnLogin" layoutX="110.0" layoutY="184.0" mnemonicParsing="false" onAction="btnLoginAction" text="Login" />
      <Button fx:id="btnReset" layoutX="232.0" layoutY="184.0" mnemonicParsing="false" onAction="btnResetAction" text="Reset" />
      <Label fx:id="lblMessage" layoutX="110.0" layoutY="236.0" prefHeight="31.0" prefWidth="187.0" />
   </children>
</AnchorPane>

I am sure the issue is with

Parent root = FXMLLoader.load(getClass().getClassLoader().getResource("Login.fxml"));

I get this error.

Executing C:\Users\David\Desktop\Java Project\loginapp\dist\run122343396\loginapp.jar using platform C:\Program Files\Java\jdk1.8.0_111\jre/bin/java
Exception in Application start method
Exception in thread "main" java.lang.reflect.InvocationTargetException
    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.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$155(LauncherImpl.java:182)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NullPointerException: Location is required.
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3207)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
    at login.Login.start(Login.java:23)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(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$148(WinApplication.java:191)
    ... 1 more
Java Result: 1

Please help me, I have looked at other's similar issues on here but no solutions worked.

James_D
  • 201,275
  • 16
  • 291
  • 322
David Frick
  • 641
  • 1
  • 9
  • 25
  • 1
    The error says "location is required", which simply means the path to your FXML file is wrong. – James_D Nov 19 '16 at 22:14
  • What would the path be, an absolute one including C:/ or a local such as /login/Login.fxml. I've tried multiple paths. – David Frick Nov 19 '16 at 22:16
  • The path is a url that is relative to the location of the current class. What package is `Login.fxml` in? (The `FXMLLoader` is expecting it in the same package as the `Login` class, i.e. in `login`.) Is the FXML file deployed to the jar file? – James_D Nov 19 '16 at 22:17
  • It is C:\Users\David\Desktop\Java Project\loginapp\src\login\Login.fxml for the full path. I've tried what most videos have as /login/Login.fxml and even the absolute path. – David Frick Nov 19 '16 at 22:18
  • 1
    Well the file path is hardly likely to make any sense after the application is deployed (you really think it's going to be able to find the `src` directory at runtime?). Is the FXML file deployed to the jar file? – James_D Nov 19 '16 at 22:20
  • No, I don't think it is. How would I change that in Netbeans – David Frick Nov 19 '16 at 22:30
  • Well then I assume that is the problem. You need to configure your IDE so the fxml file is deployed to it. – James_D Nov 19 '16 at 22:31
  • I actually do see it now in the jar directory, and it matches up with login/Login.fxml – David Frick Nov 19 '16 at 22:35
  • 2
    A duplicate of [How do I determine the correct path for FXML files, CSS files, Images, and other resources needed by my JavaFX Application?](https://stackoverflow.com/questions/61531317/how-do-i-determine-the-correct-path-for-fxml-files-css-files-images-and-other) mixed-in with a bit weirdness (two loaders, why?) – kleopatra Apr 24 '23 at 15:24

2 Answers2

6

getClass().getResource("Login.fxml") should return a non-null value, since using Class.getResource looks up the resouce relative to the class. Since you seem to have placed the resource in the login package, using a class in that package with resource name Login.fxml should work.

For some reason you do not use the FXMLLoader that you pass this resource URL to. Instead you use

getClass().getClassLoader().getResource("Login.fxml")

I.e. you use the classloader to load the file. The classloader however does not know about the class you retrieve it from and therefore tries to locate Login.fxml in the default package.

If you use a classloader, you should use the full path, i.e.

getClass().getClassLoader().getResource("login/Login.fxml")
fabian
  • 80,457
  • 12
  • 86
  • 114
  • Thank you. I have tried everything and this worked. This is a nice explanation of why it worked. Clarifies it immensely! I would add that in Netbeans the full path tends to be the packagename/file.fxml – David Frick Nov 23 '16 at 12:34
1

My fxml file wasn't at the same folder that main (neither on the same package). The only way that I got is using: getClass().getResource("/fxml/inicial.fxml");

Isa
  • 11
  • 1