Why does Apache NetBeans Null Pointer Exception give me an error in my Java FXML project?

70 Views Asked by At

When I create a new java FXML project in netbeans, a simple code is normally generated, which executes without errors but in my case this is not the case, as soon as I create the project and try to execute it it does not allow me and throws an exception, something strange is that I have a project in java fx already created that does run, and I don't know why some run and others don't. here is the exception:

`Exception in Application start method
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 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(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:907)
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(Thread.java:750)
Caused by: java.lang.NullPointerException: Location is required.
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3242)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3210)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3183)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3159)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3139)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3132)
at javafxapplication3.JavaFXApplication3.start(JavaFXApplication3.java:21)
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:185)
... 1 more
Exception running application javafxapplication3.JavaFXApplication3
C:\Users\EduardDev\Documents\NetBeansProjects\JavaFXApplication3\nbproject\build-impl.xml:1367: The      following error occurred while executing this line:
C:\Users\EduardDev\Documents\NetBeansProjects\JavaFXApplication3\nbproject\build-impl.xml:994: Java r eturned: 1
BUILD FAILED (total time: 1 second)`

This is how the packages are distributed, I have other projects where the files are like this and it runs me unlike the one that netbeans creates but it doesn't work either

this is the main class

 package javafxapplication3;

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

/**
 *
 * @author EduardDev
 */
public class JavaFXApplication3 extends Application {

@Override
public void start(Stage stage) throws Exception {
    Parent root = FXMLLoader.load(getClass().getResource("/view/FXML.fxml"));
    
    Scene scene = new Scene(root);
    
    stage.setScene(scene);
    stage.show();
    
}

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    launch(args);
}

} ` and this is the controller

`package controller;

import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.Initializable;

/**
 * FXML Controller class
 *
 * @author EduardDev
 */
public class FXMLController implements Initializable {

/**
 * Initializes the controller class.
 */
@Override
public void initialize(URL url, ResourceBundle rb) {
    // TODO
}    

}`

and this is the fxml file

` <?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="400.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml/1"          fx:controller="controller.FXMLController">

</AnchorPane>`
3

There are 3 best solutions below

1
Mike Nakis On

Of all the source code that you listed, the only line that is relevant is the last line of your code in the stack trace. That's "javafxapplication3.JavaFXApplication3.start(JavaFXApplication3.java:21)", in other words, it is the following line:

Parent root = FXMLLoader.load(getClass().getResource("/view/FXML.fxml"));

So, the error is that you passed null to FXMLLoader.load(), which expects a non-null parameter.

So, obviously, getClass().getResource("...") returned null.

So, obviously, there is no such resource in your output directory.

There is a number of reasons that could be causing this:

  • there is nothing like /view/ in your project
  • the /view/ directory is not in the same directory as your JavaFXApplication3 class
  • your build system is not configured to include *.fxml files as resources
  • your build system is not configured to look for them in subdirectories
  • your build system does not put resources in the same directory structure in the output directory.
  • etc.

We would need to see your build script/definition file to decide exactly that's wrong.

3
eduard marinez On

Thank you very much, I already found the solution, the problem is that there was a problem with the fxml file that netbeans generates. The fxml file had to be created with the scene builder and then pasted into the netbeans packages. It is a problem with new versions of netbeans 20

0
David Weber On

The author solved the problem somehow by himself, and here are my two cents:

The exceptions "Location is required" or "Location is not set" often appear, when the path to the fxml file is wrong.

Paths beginning with a "/" are always absolute paths from your projects root. All other paths are relative.