I am trying to create my first JavaFX program,using intelliJ IDEA
and i m getting this error :
Exception in Application start method
java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0
(Native Method)at
java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke
(NativeMethodAccessorImpl.java:62)at
java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke
(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at
javafx.graphics/com.sun.javafx.application.LauncherImpl.
launchApplicationWithArgs(LauncherImpl.java:464)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.
launchApplication(LauncherImpl.java:363)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0
(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke
(NativeMethodAccessorImpl.java:62)
at
java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke
(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at java.base/sun.launcher.LauncherHelper$FXHelper.main
(LauncherHelper.java:1051)
Caused by: java.lang.RuntimeException: Exception in Application
start method
at
javafx.graphics/com.sun.javafx.application.LauncherImpl.
launchApplication1(LauncherImpl.java:900)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.
lambda$launchApplication$2(LauncherImpl.java:195)
at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: java.lang.IllegalAccessError: class
com.sun.javafx.fxml.FXMLLoaderHelper (in unnamed module
#0x5ba89dd0) cannot access class com.sun.javafx.util.Utils (in
module javafx.graphics) because module javafx.graphics does not
export com.sun.javafx.util to unnamed module #0x5ba89dd0
at com.sun.javafx.fxml.FXMLLoaderHelper.<clinit>
(FXMLLoaderHelper.java:38)
at javafx.fxml.FXMLLoader.<clinit>(FXMLLoader.java:2056)
at sample.Main.start(Main.java:13)
at
javafx.graphics/com.sun.javafx.application.LauncherImpl.
lambda$launchApplication1$9(LauncherImpl.java:846)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.
lambda$runAndWait$12(PlatformImpl.java:455)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.
lambda$runLater$10(PlatformImpl.java:428)
at java.base/java.security.AccessController.doPrivileged
(Native Method)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.
lambda$runLater$11(PlatformImpl.java:427)
at
javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run
(InvokeLaterDispatcher.java:96)
at javafx.graphics/com.sun.glass.ui.gtk.GtkApplication._runLoop
(Native Method)
at
javafx.graphics/com.sun.glass.ui.gtk.GtkApplication.
lambda$runLoop$11(GtkApplication.java:277)
... 1 more
Exception running application sample.Main
This is my FXML file :
`<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane prefHeight="386.0" prefWidth="621.0"
xmlns="http://javafx.com/javafx/8.0.172-ea"
xmlns:fx="http://javafx.com/fxml" fx:controller="sample.Controller">
<children>
<Label fx:id="m" layoutX="282.0" layoutY="192.0" text="this is a
test " />
</children>
</AnchorPane>`
main.java :
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("Main.fxml"));
primaryStage.setTitle("Hello World");
primaryStage.setScene(new Scene(root, 300, 275));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}}
this is the project structure :
project structure
I even changet the fxml file location but he keep on getting the same error.
the FXML file and Main.java are in the same package
i am Ubuntu
i have found the solution , i have added the JavaFX SDK to the modules and modified the module-info.java file because i am using jdk 11
module project-name {
requires javafx.fxml;
requires javafx.controls;
opens sample;
}
Related
I've got a problem with adding an fxml file to my JavaFX project. The onAction function in my FXML file doesn't work.
The IntelliJ IDE colored this onAction function red and I can't use it in the controller. Do you know the reason?
Here is code in fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/17" xmlns:fx="http://javafx.com/fxml/1">
<children>
<Button fx:id="Button" layoutX="211.0" layoutY="127.0" mnemonicParsing="false" onAction="#btnClicked" text="Button" AnchorPane.bottomAnchor="247.4" AnchorPane.leftAnchor="211.0" AnchorPane.rightAnchor="337.0" AnchorPane.topAnchor="127.0" />
</children>
</AnchorPane>
Code in Hello class:
package paczka.p2;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
public class HelloController {
#FXML
private Label welcomeText;
#FXML
protected void onHelloButtonClick() {
welcomeText.setText("Welcome to JavaFX Application!");
}
}
Code in Controller class:
package paczka.p2;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.stage.Stage;
import java.io.IOException;
public class HelloApplication extends Application {
#Override
public void start(Stage stage) throws IOException {
FXMLLoader fxmlLoader = new FXMLLoader(HelloApplication.class.getResource("hello-view.fxml"));
Scene scene = new Scene(fxmlLoader.load(), 320, 240);
stage.setTitle("Hello!");
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch();
}
}
Error in console:
Exception in Application start method
java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:568)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:465)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:364)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:568)
at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1071)
Caused by: java.lang.RuntimeException: Exception in Application start method
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:901)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:196)
at java.base/java.lang.Thread.run(Thread.java:833)
Caused by: javafx.fxml.LoadException: No controller specified.
/D:/user_Folder/Projekty/P2/target/classes/paczka/p2/hello-view.fxml:9
at javafx.fxml/javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2703)
at javafx.fxml/javafx.fxml.FXMLLoader$Element.getControllerMethodHandle(FXMLLoader.java:568)
at javafx.fxml/javafx.fxml.FXMLLoader$Element.processEventHandlerAttributes(FXMLLoader.java:610)
at javafx.fxml/javafx.fxml.FXMLLoader$ValueElement.processEndElement(FXMLLoader.java:781)
at javafx.fxml/javafx.fxml.FXMLLoader.processEndElement(FXMLLoader.java:2924)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2639)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548)
at javafx.fxml/javafx.fxml.FXMLLoader.load(FXMLLoader.java:2516)
at paczka.p2/paczka.p2.HelloApplication.start(HelloApplication.java:14)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:847)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:484)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:457)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:456)
at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:184)
... 1 more
Exception running application paczka.p2.HelloApplication
Process finished with exit code 1
Your issue starts in the hello-view.fxml file.
In your anchor pane, you need an fx:controller attribute.
It should look something like this:
fx:controller="HelloController" <!-- Assumes all folders in the same directory. -->
fx:controller="controller.HelloController" <!-- Assumes `HelloController.java` is located in `src > controller -->
Once you've set hello-view.fxml to properly look for HelloController.java, you want to visit your button element and set its onAction attribute to a function inside the controller.
Simply put, your Controller's function is onHelloButtonClick(), so the button's attribute should be:
onAction="#onHelloButtonClick()"
Now, as it currently stands, your controller's function sets a text element that doesn't exist in your .fxml file. You'd want to add this:
<Text fx:id="welcomeText"></Text>
Finally, HelloController.java needs a variable for each element you're interacting with. That means one for your button and one for your text.
Their names are decided by the fx:id attributes you set in hello-View.fxml
public Text welcomeText;
public Button Button;
In the end, this all comes together as follows:
Your Java program starts with the main function which, as you wrote, finds and reveals a hello-view.fxml. That .fxml file sets a fx:controller="HelloController attribute in its anchorpane, that specificies for all of its functions to come from HelloController.java.
Each element in hello-view.fxml has an fx:id attribute designating the name of a variable that must be listed in in HelloController.java.
Elements in hello-view.fxml need an onAction attribute, matched to the exact name of a function in the element's controller (as you specified with fx:controller).
From there, HelloController.java has a variable for each element its points to in the .fxml file that called it, and any function in the controller points to its variables, where java simply knows what to do from there.
I am trying to build an app in JavaFX but i am facing the issue.
Below code generating error.I error are showing since i created the project.
I downloaded the JavaFX 14 from "https://gluonhq.com/products/javafx/" and include all jar files present in lib directory.
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("sample.fxml"));
primaryStage.setTitle("Hello World");
primaryStage.setScene(new Scene(root));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
But This code works fine
package sample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class Main extends Application {
#Override
public void start(Stage primaryStage) throws Exception{
//Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
StackPane root=new StackPane();
primaryStage.setTitle("Hello World");
primaryStage.setScene(new Scene(root));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Getting this error:
"C:\Program Files\Java\jdk-14.0.1\bin\java.exe" --add-modules javafx.base,javafx.graphics --add-reads javafx.base=ALL-UNNAMED --add-reads javafx.graphics=ALL-UNNAMED "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2020.1.3\lib\idea_rt.jar=60984:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2020.1.3\bin" -Dfile.encoding=UTF-8 -classpath E:\Shapes\out\production\Shapes;E:\Software\javafx-sdk-14.0.2.1\lib\javafx-swt.jar;E:\Software\javafx-sdk-14.0.2.1\lib\javafx.base.jar;E:\Software\javafx-sdk-14.0.2.1\lib\javafx.controls.jar;E:\Software\javafx-sdk-14.0.2.1\lib\javafx.fxml.jar;E:\Software\javafx-sdk-14.0.2.1\lib\javafx.graphics.jar;E:\Software\javafx-sdk-14.0.2.1\lib\javafx.media.jar;E:\Software\javafx-sdk-14.0.2.1\lib\javafx.swing.jar;E:\Software\javafx-sdk-14.0.2.1\lib\javafx.web.jar -p E:\Software\javafx-sdk-14.0.2.1\lib\javafx.base.jar;E:\Software\javafx-sdk-14.0.2.1\lib\javafx.graphics.jar sample.Main
Exception in Application start method
java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:464)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:363)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1051)
Caused by: java.lang.RuntimeException: Exception in Application start method
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:900)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:195)
at java.base/java.lang.Thread.run(Thread.java:832)
Caused by: java.lang.IllegalAccessError: class com.sun.javafx.fxml.FXMLLoaderHelper (in unnamed module #0x66dac2b5) cannot access class com.sun.javafx.util.Utils (in module javafx.graphics) because module javafx.graphics does not export com.sun.javafx.util to unnamed module #0x66dac2b5
at com.sun.javafx.fxml.FXMLLoaderHelper.<clinit>(FXMLLoaderHelper.java:38)
at javafx.fxml.FXMLLoader.<clinit>(FXMLLoader.java:2056)
at sample.Main.start(Main.java:14)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:846)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:455)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
... 1 more
Exception running application sample.Main
Process finished with exit code 1
The fundamental error is (formatted for readability):
Caused by: java.lang.IllegalAccessError:
class com.sun.javafx.fxml.FXMLLoaderHelper (in unnamed module #0x66dac2b5)
cannot access class com.sun.javafx.util.Utils (in module javafx.graphics)
because module javafx.graphics does not export com.sun.javafx.util to unnamed module #0x66dac2b5
That error is telling you that the javafx.fxml module has ended up on the class-path while the javafx.graphics module is on the module-path. This scenario prevents the special access to internal code the javafx.graphics module grants to the javafx.fxml module.
The solution to this is to do one of the following:
Include javafx.fxml in your --add-modules VM argument.
Make your own code modular, add the necessary requires, exports, and opens directives, and launch your application with --module.
If you're curious about how the above error comes about then my answer to another question goes into more detail. Note it focuses on the javafx.media module but the concept is the same.
I'm starting my way with JavaFXml an i have problem with compile my program. Compiler show me problem and i don't have any idea to solve this.
My Mine class:
package com.biku.readerFX;
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("sample.fxml"));
primaryStage.setTitle("Hello World");
primaryStage.setScene(new Scene(root, 300, 275));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
My Controller class:
package com.biku.readerFX;
public class Controller {
}
My sample.fxml file
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<VBox prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml"
fx:controller="com.biku.readerFX.Controller">
<Label text="I love bacon"/>
<Button text="Submit"/>
</VBox>
And when I compile this, compiller show me an errors:
/usr/lib/jvm/java-11-openjdk-amd64/bin/java --module-path /home/biku/Pobrane/openjfx-13/javafx-sdk-13/lib --add-modules javafx.controls,javafx.fxml --add-modules javafx.base,javafx.graphics --add-reads javafx.base=ALL-UNNAMED --add-reads javafx.graphics=ALL-UNNAMED -javaagent:/snap/intellij-idea-community/177/lib/idea_rt.jar=41763:/snap/intellij-idea-community/177/bin -Dfile.encoding=UTF-8 -classpath /home/biku/IdeaProjects/WordReader/target/classes:/usr/lib/jvm/java-1.11.0-openjdk-amd64:/home/biku/.m2/repository/org/apache/maven/plugins/maven-compiler-plugin/3.8.1/maven-compiler-plugin-3.8.1.jar:/home/biku/.m2/repository/org/apache/maven/maven-plugin-api/3.0/maven-plugin-api-3.0.jar:/home/biku/.m2/repository/org/apache/maven/maven-model/3.0/maven-model-3.0.jar:/home/biku/.m2/repository/org/sonatype/sisu/sisu-inject-plexus/1.4.2/sisu-inject-plexus-1.4.2.jar:/home/biku/.m2/repository/org/sonatype/sisu/sisu-inject-bean/1.4.2/sisu-inject-bean-1.4.2.jar:/home/biku/.m2/repository/org/sonatype/sisu/sisu-guice/2.1.7/sisu-guice-2.1.7-noaop.jar:/home/biku/.m2/repository/org/apache/maven/maven-artifact/3.0/maven-artifact-3.0.jar:/home/biku/.m2/repository/org/codehaus/plexus/plexus-utils/2.0.4/plexus-utils-2.0.4.jar:/home/biku/.m2/repository/org/apache/maven/maven-core/3.0/maven-core-3.0.jar:/home/biku/.m2/repository/org/apache/maven/maven-settings/3.0/maven-settings-3.0.jar:/home/biku/.m2/repository/org/apache/maven/maven-settings-builder/3.0/maven-settings-builder-3.0.jar:/home/biku/.m2/repository/org/apache/maven/maven-repository-metadata/3.0/maven-repository-metadata-3.0.jar:/home/biku/.m2/repository/org/apache/maven/maven-model-builder/3.0/maven-model-builder-3.0.jar:/home/biku/.m2/repository/org/apache/maven/maven-aether-provider/3.0/maven-aether-provider-3.0.jar:/home/biku/.m2/repository/org/sonatype/aether/aether-impl/1.7/aether-impl-1.7.jar:/home/biku/.m2/repository/org/sonatype/aether/aether-spi/1.7/aether-spi-1.7.jar:/home/biku/.m2/repository/org/sonatype/aether/aether-api/1.7/aether-api-1.7.jar:/home/biku/.m2/repository/org/sonatype/aether/aether-util/1.7/aether-util-1.7.jar:/home/biku/.m2/repository/org/codehaus/plexus/plexus-interpolation/1.14/plexus-interpolation-1.14.jar:/home/biku/.m2/repository/org/codehaus/plexus/plexus-classworlds/2.2.3/plexus-classworlds-2.2.3.jar:/home/biku/.m2/repository/org/codehaus/plexus/plexus-component-annotations/1.5.5/plexus-component-annotations-1.5.5.jar:/home/biku/.m2/repository/org/sonatype/plexus/plexus-sec-dispatcher/1.3/plexus-sec-dispatcher-1.3.jar:/home/biku/.m2/repository/org/sonatype/plexus/plexus-cipher/1.4/plexus-cipher-1.4.jar:/home/biku/.m2/repository/org/apache/maven/shared/maven-shared-utils/3.2.1/maven-shared-utils-3.2.1.jar:/home/biku/.m2/repository/commons-io/commons-io/2.5/commons-io-2.5.jar:/home/biku/.m2/repository/org/apache/maven/shared/maven-shared-incremental/1.1/maven-shared-incremental-1.1.jar:/home/biku/.m2/repository/org/codehaus/plexus/plexus-java/0.9.10/plexus-java-0.9.10.jar:/home/biku/.m2/repository/org/ow2/asm/asm/6.2/asm-6.2.jar:/home/biku/.m2/repository/com/thoughtworks/qdox/qdox/2.0-M8/qdox-2.0-M8.jar:/home/biku/.m2/repository/org/codehaus/plexus/plexus-compiler-api/2.8.4/plexus-compiler-api-2.8.4.jar:/home/biku/.m2/repository/org/codehaus/plexus/plexus-compiler-manager/2.8.4/plexus-compiler-manager-2.8.4.jar:/home/biku/.m2/repository/org/codehaus/plexus/plexus-compiler-javac/2.8.4/plexus-compiler-javac-2.8.4.jar:/home/biku/.m2/repository/org/openjfx/javafx-controls/12.0.2/javafx-controls-12.0.2.jar:/home/biku/.m2/repository/org/openjfx/javafx-controls/12.0.2/javafx-controls-12.0.2-linux.jar:/home/biku/.m2/repository/org/openjfx/javafx-graphics/12.0.2/javafx-graphics-12.0.2.jar:/home/biku/.m2/repository/org/openjfx/javafx-graphics/12.0.2/javafx-graphics-12.0.2-linux.jar:/home/biku/.m2/repository/org/openjfx/javafx-base/12.0.2/javafx-base-12.0.2.jar:/home/biku/.m2/repository/org/openjfx/javafx-base/12.0.2/javafx-base-12.0.2-linux.jar:/home/biku/.m2/repository/org/openjfx/javafx-fxml/13/javafx-fxml-13.jar:/home/biku/.m2/repository/org/openjfx/javafx-fxml/13/javafx-fxml-13-linux.jar:/home/biku/.m2/repository/org/openjfx/javafx-maven-plugin/0.0.3/javafx-maven-plugin-0.0.3.jar:/home/biku/.m2/repository/org/codehaus/plexus/plexus-archiver/3.6.0/plexus-archiver-3.6.0.jar:/home/biku/.m2/repository/org/codehaus/plexus/plexus-io/3.0.1/plexus-io-3.0.1.jar:/home/biku/.m2/repository/org/apache/commons/commons-compress/1.16.1/commons-compress-1.16.1.jar:/home/biku/.m2/repository/org/objenesis/objenesis/2.6/objenesis-2.6.jar:/home/biku/.m2/repository/org/iq80/snappy/snappy/0.4/snappy-0.4.jar:/home/biku/.m2/repository/org/tukaani/xz/1.8/xz-1.8.jar:/home/biku/.m2/repository/org/apache/commons/commons-exec/1.3/commons-exec-1.3.jar:/home/biku/.m2/repository/org/twdata/maven/mojo-executor/2.3.0/mojo-executor-2.3.0.jar:/home/biku/.m2/repository/org/slf4j/slf4j-api/1.7.22/slf4j-api-1.7.22.jar:/home/biku/.m2/repository/org/slf4j/slf4j-simple/1.7.22/slf4j-simple-1.7.22.jar com.biku.readerFX.Main
Exception in Application start method
java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:464)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:363)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1051)
Caused by: java.lang.RuntimeException: Exception in Application start method
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:900)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:195)
at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: java.lang.NullPointerException: Location is required.
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3230)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3194)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3163)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3136)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3113)
at javafx.fxml/javafx.fxml.FXMLLoader.load(FXMLLoader.java:3106)
at com.biku.readerFX.Main.start(Main.java:13)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:846)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:455)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
at javafx.graphics/com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.gtk.GtkApplication.lambda$runLoop$11(GtkApplication.java:277)
... 1 more
Exception running application com.biku.readerFX.Main
Process finished with exit code 1
I try solve it like on this page:
https://github.com/openjfx/javafx-maven-plugin
https://openjfx.io/openjfx-docs/#maven
And add on VM options path to fx: --module-path ${PATH_TO_FX} --add-modules javafx.controls,javafx.fxml
Add javafx like lib to project.
Have u any idea to help me ? thx
Whichever way your project is setup, the following line:
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
is unable to fetch "sample.fxml". Make sure you get the location of your file/specification of your path right, otherwise you'll always run into this error message (located towards the bottom of your stack trace):
Caused by: java.lang.NullPointerException: Location is required.
tl;dr: Your path for "sample.fxml" is wrong. You're probably going to have to move it (the sample.fxml file) into the same package as your Main.java file, or maybe your src/main/resources/ directory for a Maven setup. I'm not entirely familiar with Gradle, but you'd have to put the fxml file in the resources root there as well.
Ok i have solution, the path is correct but I didn't have a file "Project_Name".iml. This file must be the same level like pom.xml, directly in project folder. And inside this file is somthing like that:
<?xml version="1.0" encoding="UTF-8"?>
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_11">
<output url="file://$MODULE_DIR$/target/classes" />
<output-test url="file://$MODULE_DIR$/target/test-classes" />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
This is the test.fxml:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.BorderPane?>
<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1">
<center>
<Label text="It is all OK" BorderPane.alignment="CENTER" />
</center>
</BorderPane>
I did a Main.java that launchs the window, like follow:
import java.io.IOException;
import java.net.URL;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Parent;
import javafx.scene.Scene;
public class Main extends Application {
public static void main(String args[]) {
launch(args);
}
public void start(Stage stage) {
try {
URL url = this.getClass().getResource("/test.fxml");
Parent root = FXMLLoader.load(url);
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
} catch (Exception e) {
// ignore
}
}
}
It works.
But the main.py just can not launch and it is almost equal to Main.java.
I did 2 versions of this file that launchs different errors.
This is the first version:
import sys
from java.io import IOException
from java.net import URL
from javafx.application import Application
from javafx.fxml import FXMLLoader
from javafx.stage import Stage
from javafx.scene import Parent
from javafx.scene import Scene
class Main(Application):
#classmethod
def main(cls, args):
Main.launch(cls, args)
def start(self, stage):
try:
url = self.getClass().getResource('/test.fxml')
root = FXMLLoader.load(url)
scene = Scene(root)
stage.setScene(scene)
stage.show()
except Exception as exc:
pass
if __name__ == '__main__':
Main.main(sys.argv)
That shows:
Exception in Application start method
Traceback (most recent call last):
File "/home/flima/dev/db/herbalife/sell/main.py", line 48, in
Main.main(sys.argv)
File "/home/flima/dev/db/herbalife/sell/main.py", line 29, in main
Main.launch(cls, args)
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$154(LauncherImpl.java:182)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.NullPointerException
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)
java.lang.RuntimeException: java.lang.RuntimeException: Exception in Application start method
This is the second version:
import sys
from java.io import IOException
from java.net import URL
from javafx.application import Application
from javafx.fxml import FXMLLoader
from javafx.stage import Stage
from javafx.scene import Parent
from javafx.scene import Scene
class Main(Application):
#classmethod
def main(cls, args):
Main.launch(cls, args)
def start(self, stage):
try:
url = self.getClass().getResource('/test.fxml')
self.loader = FXMLLoader(url) # changed here
root = self.loader.load() # changed here
scene = Scene(root)
stage.setScene(scene)
stage.show()
except Exception as exc:
pass
if __name__ == '__main__':
Main.main(sys.argv)
I get another error, that is:
Exception in Application start method
Traceback (most recent call last):
File "/home/flima/dev/db/herbalife/sell/main.py", line 48, in
Main.main(sys.argv)
File "/home/flima/dev/db/herbalife/sell/main.py", line 29, in main
Main.launch(cls, args)
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$154(LauncherImpl.java:182)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.IllegalStateException: Location is not set.
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2434)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2409)
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)
java.lang.RuntimeException: java.lang.RuntimeException: Exception in Application start method
Where is my mistake?
All the files are in the same folder.
Please, help me. Thanks.
Jython version is:
Jython 2.7.0 (default:9987c746f838, Apr 29 2015, 02:25:11)
Java version is:
java version "1.8.0_191"
Java(TM) SE Runtime Environment (build 1.8.0_191-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.191-b12, mixed mode)
I am running the code at Lubuntu based on Ubuntu 18.04.1 LTS.
I have little experience working with Scene Builder and fxml and today when I started working on my new project I ran into an issue with setting my fxml file location. I'm also using NetBeans 8.2.
Here is my application class:
package javafxapplication;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
/**
*
* #author Ben
*/
public class JavaFXApplication extends Application {
#Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("FXMLDocumentLoad.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);
}
}
My FXML:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane id="AnchorPane" prefHeight="200" prefWidth="320" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/9.0.1" fx:controller="javafxapplication.FXMLDocumentControllerTest">
<children>
<Label fx:id="label" layoutX="126" layoutY="120" minHeight="16" minWidth="69" />
</children>
</AnchorPane>
Lastly, my Controller:
package javafxapplication;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
/**
*
* #author Ben
*/
public class FXMLDocumentControllerTest implements Initializable {
#FXML
private Label label;
#Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
}
Error:
ant -f C:\\Users\\Ben\\Do7cuments\\NetBeansProjects\\JavaFXApplication -Djavac.includes=javafxapplication/JavaFXApplication.java -Dnb.internal.action.name=run.single -Drun.class=javafxapplication.JavaFXApplication run-single
init:
Deleting: C:\Users\Ben\Do7cuments\NetBeansProjects\JavaFXApplication\build\built-jar.properties
deps-jar:
Updating property file: C:\Users\Ben\Do7cuments\NetBeansProjects\JavaFXApplication\build\built-jar.properties
Compiling 1 source file to C:\Users\Ben\Do7cuments\NetBeansProjects\JavaFXApplication\build\classes
compile-single:
run-single:
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: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:748)
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 javafxapplication.JavaFXApplication.start(JavaFXApplication.java:22)
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
Exception running application javafxapplication.JavaFXApplication
C:\Users\Ben\Do7cuments\NetBeansProjects\JavaFXApplication\nbproject\build-impl.xml:1052: The following error occurred while executing this line:
C:\Users\Ben\Do7cuments\NetBeansProjects\JavaFXApplication\nbproject\build-impl.xml:806: Java returned: 1
BUILD FAILED (total time: 0 seconds)
All of my classes are in the same folder "/src/javafxapplication"
I have tried changing the getResource to "/FXMLDocumentLoad.fxml" and "/javafxapplication/FXMLDocumentLoad.fxml", but neither worked
Thanks!
It turned out to be a NetBeans 8.2 bug. The solution is to run the file using the run button rather than doing shift-F6 (pc).
If you use a classloader, you should use the full path:
getClass().getClassLoader().getResource("/...path.../file.fxml")
You get a null value from getClass().getResource("file.fxml") because using Class.getResource looks up the resource relative to the class.
All of my classes are in the same folder
FXMLLoader.load(getClass().getResource("FXMLDocumentLoad.fxml"));
classloader tries to locate FXMLDocumentLoad.fxml in the default package.