JavaFX java.lang.IllegalStateException: Location is not set - javafx

Help! I'm stuck.. I try to run my main javafx app
Here is my codes;
#Override
public void start(Stage primaryStage) throws Exception {
try {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("/com/utmkl/fxml/SimulatorDisplay.fxml"));
Parent content = (Parent)loader.load();
primaryStage.setResizable(false);
primaryStage.initStyle(StageStyle.UTILITY);
primaryStage.setScene(new Scene(content));
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
Below is my folder structure:
picture Folder structure
Below is the error
java.lang.IllegalStateException: Location is not set.
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2434)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2409)
at com.utmkl.VMCSManager.start(VMCSManager.java:43)
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)
at java.lang.Thread.run(Thread.java:745)

I've found the answer..
the resource need to be in the same folder structure as main class
Here is my Maven-JavaFX Folder structure
VMCS
- src/main/java
- com.utmkl
VMCSManager.java
- src/main/resources
- com.utmkl.fxml
SimulatorDisplay.fxml
So, the correct code (which success to run)
VMCSManager.java
#Override
public void start(Stage primaryStage) throws Exception {
try {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("fxml/SimulatorDisplay.fxml"));
Parent content = loader.load();
Scene scene = new Scene(content);
primaryStage.setResizable(false);
primaryStage.initStyle(StageStyle.UTILITY);
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}

Even though you do set a location with loader.setLocation(getClass().getResource("/com/utmkl/fxml/ControllerDisplay.fxml")); it is likely that the resource URL doesn't actually refer to an existing resource on the classpath.
If you add System.out.println(getClass().getResource("/com/utmkl/fxml/ControllerDisplay.fxml")); to your code I think it will print null as a result.

Related

InvocationTargetException with javafx launch

I have some trouble with launching a javafx application. To explain, I wrote a code that worked but I was on Ubuntu 19.04 and I decided to reinstall my Linux version (20.04). I copied my code and reinstalled e(fx)clipse, scenebuilder, openjfx, and imported the same jfxrt.jar but nothing worked. I can't manage to launch javafx application, even if the code is empty (ig code). Oh and I have to use java 8 for this homework.
Can you help me please?
Here is my code
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.FlowPane;
import javafx.stage.Stage;
public class LaunchApplication extends Application {
public static void main(String[] args) {
launch(args);
}
//Empty but still error
#Override
public void start(Stage arg0) throws Exception {
}
//Old code, doesnt work anymore
// #Override
// public void start(Stage primaryStage) throws Exception {
// try {
//
// FXMLLoader loader = new FXMLLoader();
// loader.setLocation(getClass().getResource("view/Login.fxml"));
// Parent root = loader.load();
//
// Scene scene = new Scene(root);
// primaryStage.setScene(scene);
// primaryStage.setResizable(true);
// primaryStage.show();
//
// } catch (Exception e) {
// System.out.println(e);
// }
//
// }
}
And there my error when running it
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: java.lang.NoSuchMethodError: <init>
at com.sun.javafx.tk.quantum.QuantumToolkit.startup(QuantumToolkit.java:288)
at com.sun.javafx.application.PlatformImpl.startup(PlatformImpl.java:211)
at com.sun.javafx.application.LauncherImpl.startToolkit(LauncherImpl.java:675)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:337)
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
... 5 more
Caused by: java.lang.NoSuchMethodError: <init>
at java.lang.ClassLoader$NativeLibrary.load(Native Method)
at java.lang.ClassLoader.loadLibrary0(ClassLoader.java:1934)
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1817)
at java.lang.Runtime.load0(Runtime.java:810)
at java.lang.System.load(System.java:1088)
at com.sun.glass.utils.NativeLibLoader.loadLibraryInternal(NativeLibLoader.java:107)
at com.sun.glass.utils.NativeLibLoader.loadLibrary(NativeLibLoader.java:39)
at com.sun.glass.ui.gtk.GtkApplication.lambda$new$5(GtkApplication.java:156)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.glass.ui.gtk.GtkApplication.<init>(GtkApplication.java:140)
at com.sun.glass.ui.gtk.GtkPlatformFactory.createApplication(GtkPlatformFactory.java:41)
at com.sun.glass.ui.Application.run(Application.java:146)
at com.sun.javafx.tk.quantum.QuantumToolkit.startup(QuantumToolkit.java:278)
... 9 more
```

Rerun JavaFX application

I'm developing a plugin for IntelliJ. It is a menu that has only one option, and clicking it starts a JavaFX application. The application starts normally, but when I close the application and start again I get the following error:
java.lang.IllegalStateException: Application launch must not be called more than once
It's a simple application, the code is as follows:
This is the code in the menu.
try {
Application.launch(MyApplication.class);
} catch (Exception e) {
e.printStackTrace();
}
This is the JavaFX Application.
public class MyApplication extends Application {
#Override
public void start(Stage primaryStage) throws Exception {
try {
FXMLLoader loader = new FXMLLoader(getClass().getResource("/fxml/main-window.fxml"));
loader.setController(new MainController());
AnchorPane root = loader.load();
Scene scene = new Scene(root, 1000, 600);
primaryStage.setScene(scene);
primaryStage.setMaximized(false);
primaryStage.setResizable(false);
primaryStage.show();
} catch (Exception e) {
e.printStackTrace();
}
}
}
How can I rerun the application? I tried to follow some answers here on the site, but I did not understand them very well, nor could I execute them correctly.

JavaFX NullPointerException Button in other class

I've to create an application for a school project. After you login, you should go to the dashboard. That works, but, it throws a NullPointerException when I try to set the button do disabled.
In this file, the stage is changing (after logging in):
public class ScreenController {
public void setScene(Stage stage, Parent root,Button button, String file){
if(file == "dashboard"){
stage = (Stage) button.getScene().getWindow();
try {
root = FXMLLoader.load(getClass().getResource("Dashboard.fxml"));
} catch (IOException ex) {
System.out.println("IOException: "+ex);
}
Scene scene = new Scene(root);
stage.setTitle("Corendon - Dashboard");
stage.setScene(scene);
stage.show();
Dashboard dashboard = new Dashboard();
}
}
}
And on the last line, the button should be set to disabled...
Dashboard dashboard = new Dashboard();
... by this class:
public class Dashboard extends ScreenController {
#FXML public Button buttonDashboard1;
public Dashboard(){
buttonDashboard1.setDisable(true);
}
}
But that is not working it throws the NullPointerException:
Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1774)
at
...
Caused by: java.lang.NullPointerException
at fastenyourseatbelts.Dashboard.<init>(Dashboard.java:11)
at fastenyourseatbelts.ScreenController.setScene(ScreenController.java:33)
at fastenyourseatbelts.LoginController.buttonLogin(LoginController.java:74)
... 59 more
I'm trying for hours now, but I don't get the solution... Does anyone know what is going wrong and why?
Move the code from the constructor of your controller to the initialize. This method is invoked by the FXMLLoader, after injection of all fields is completed and therefore should have access to the buttonDashboard1 instance:
public class Dashboard extends ScreenController {
#FXML public Button buttonDashboard1;
#FXML
private void initialize() {
buttonDashboard1.setDisable(true);
}
}
You also have to make sure the controller is either specified in the root element of the fxml file e.g. (replace packagename with the package of the Dashboard class)
<VBox xmlns:fx="http://javafx.com/fxml/1" fx:controller="packagename.Dashboard">
<children>
<Button text="click me" fx:id="buttonDashboard1"/>
</children>
</VBox>
or set as controller for the fxml before loading it:
FXMLLoader loader = new FXMLLoader(getClass().getResource("Dashboard.fxml"));
Dashboard dashboard = new Dashboard();
loader.setController(dashboard);
root = loader.load();
(don't use a fx:controller attribute in this case)

What is wrong with this FX Application?

I am hours trying to know why this code is wrong, it gives me an Exception, the GUI.fxml is on the root of the project.
public class MyApp extends Application{
#Override
public void start(Stage primaryStage) throws Exception {
String location = "/GUI.fxml";
Parent root = FXMLLoader.load(getClass().getResource(location));
primaryStage.setScene(new Scene(root,300,450));
primaryStage.setTitle("Minha Janela");
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Already searched a lot, no solution found yet.
Exception in Application start method
Exception in thread "main" java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:403)
at com.sun.javafx.application.LauncherImpl.access$000(LauncherImpl.java:47)
at com.sun.javafx.application.LauncherImpl$1.run(LauncherImpl.java:115)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NullPointerException: Location is required.
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2825)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2809)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2795)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2782)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2771)
at g1.MyApp.start(MyApp.java:13)
at com.sun.javafx.application.LauncherImpl$5.run(LauncherImpl.java:319)
at com.sun.javafx.application.PlatformImpl$5.run(PlatformImpl.java:219)
at com.sun.javafx.application.PlatformImpl$4$1.run(PlatformImpl.java:182)
at com.sun.javafx.application.PlatformImpl$4$1.run(PlatformImpl.java:179)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl$4.run(PlatformImpl.java:179)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:76)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.access$100(WinApplication.java:17)
at com.sun.glass.ui.win.WinApplication$3$1.run(WinApplication.java:67)
... 1 more
The FXML should not be in the root of the project, but in the classpath. Try relocating the fxml into a source folder.
For general projects, you have a src folder. You can also create your own custom source folder also.
For maven projects, you can try keeping them inside src/main/resources.

JavaFX Use one Loader load two different nodes

Everyone.I would like to ask, Can we use one loader to load two different FXMLs at one time? Actually, I want to know whether can I load two fxmls, so that these two fxmls can be displayed at the same time, at one screen.
public class NodeLink extends Application {
#Override
public void start(Stage stage) throws Exception {
FXMLLoader loader = new FXMLLoader();
AnchorPane root = (AnchorPane)loader.load(getClass().getResource("StartPanel.fxml"));
Group centralNode = (Group)loader.load(this.getClass().getResource("CentralNode.fxm."));
root.getChildren().setAll(centralNode);
//Group centralNode = FXMLLoader.load(getClass().getResource("CentralNode.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
As above, Group centralNode = ... this statement will cause error, as follows:
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:483)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:367)...
So, why this will cause errors? Can we use several fxmls once to make up a screen?
Thank you so much!

Resources