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 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>`
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:
So, the error is that you passed
nulltoFXMLLoader.load(), which expects a non-null parameter.So, obviously,
getClass().getResource("...")returnednull.So, obviously, there is no such resource in your output directory.
There is a number of reasons that could be causing this:
/view/in your project/view/directory is not in the same directory as yourJavaFXApplication3class*.fxmlfiles as resourcesWe would need to see your build script/definition file to decide exactly that's wrong.