1

I am making a ToDo App in JavaFX but it says this error (even without the database, login system just want to run it plainly): I like to code but not when errors come up Thats why i am called ErrorLane

Main.java:

package sample;

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

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception{
        Parent root = FXMLLoader.load(getClass().getResource("C:\\Users\\arhaan\\eclipse-workspace\\ToDoApp\\src\\sample\\viewlogin.fxml"));
        primaryStage.setTitle("Hello World");
        primaryStage.setScene(new Scene(root, 300, 275));
        primaryStage.show();
    }


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

login.fxml:

<?import javafx.geometry.Insets?>
<?import javafx.scene.layout.GridPane?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<GridPane fx:controller="sample.Controller"
          xmlns:fx="http://javafx.com/fxml" fx:controller="application.FXMLDocumentController" alignment="center" hgap="10" vgap="10">
</GridPane>

It is in Eclipse not IntelliJ IDEA with JRE 1.8.0

This is the error:

Exception in Application start method
java.lang.reflect.InvocationTargetException
    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 com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
    at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
    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.launcher.LauncherHelper$FXHelper.main(Unknown Source)
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$1(LauncherImpl.java:182)
    at java.lang.Thread.run(Unknown Source)
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 sample.Main.start(Main.java:13)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$8(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$7(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$5(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$6(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$4(WinApplication.java:186)
    ... 1 more
Exception running application sample.Main
ErrorLane
  • 53
  • 7
  • Does this answer your question? [JavaFX "Location is required." even though it is in the same package](https://stackoverflow.com/questions/20507591/javafx-location-is-required-even-though-it-is-in-the-same-package) – JCWasmx86 Jul 30 '20 at 05:36
  • @JCWasmx86: No i move it into sample.view but it still had the same error but only with sample.view – ErrorLane Jul 30 '20 at 05:43

1 Answers1

1

The "error" is the NPE caused by trying to use an absolute path as argument to FXMLLoader. FXMLLoader takes a URL, so to convert your absolute path to a URL that can be passed to FXMLLoader, either use a FileInputStream, or convert to a URL:

FileInputStream:

 Parent root = FXMLLoader.load(new FileInputStream("C:\\Users\\arhaan\\eclipse-workspace\\ToDoApp\\src\\sample\\login.fxml"));

Converting to URL:

Parent root = FXMLLoader.load(Paths.get("C:\\Users\\arhaan\\eclipse-workspace\\ToDoApp\\src\\sample\\login.fxml").toUri().toURL());

This answers your question, but this is bad design. Use proper structure for your source, and have the .fxml in the class path in the same package, either directly with the controller, or in a dedicated resource directory contains same package layout. With a hardcoded absolute path, your code is not portable, and will require anyone else to have the same exact path on their system to run your code.

b3tuning
  • 337
  • 2
  • 8
  • No dosen't work – ErrorLane Jul 30 '20 at 05:54
  • sorry, I copy pasted too soon, edited to remove unneeded getClass().getResource() – b3tuning Jul 30 '20 at 05:56
  • That too dosen't work – ErrorLane Jul 30 '20 at 06:02
  • Do you have an actual file at that path location called viewlogin.fxml ? – b3tuning Jul 30 '20 at 06:04
  • that is called login.fxml because i forgot the \\ but that when i tried your code with the \\ it dosen't work too. – ErrorLane Jul 30 '20 at 06:06
  • you are trying to load a file called `viewlogin.fxml`. but your file is `login.fxml` ? then simply either change the filename to match what you want to load, or edit my answers and remove the `view` – b3tuning Jul 30 '20 at 06:08
  • no i did full path but without it it also did not work – ErrorLane Jul 30 '20 at 06:12
  • what is the name of your .fxml file? and where is it located? If the file name is `login.fxml` and it is located on your C: drive at `C:\Users\arhaan\eclipse-workspace\ToDoApp\src\sample` then check my last edit of the answer. If you still have issues, then something else is going on, I will delete my answer and comments to clean up this post – b3tuning Jul 30 '20 at 06:17