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.
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 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;
}
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.
I'm in need of doing surgery to the JavaFX WebView class to make it render even when not visible. In my quest to achieve this I found Javassist, but when I try to use it, I get this error:
java.lang.IllegalArgumentException: Can not set javafx.scene.web.WebView field sample.Controller.webView to javafx.scene.web.WebView
I think that's because the member was defined before it got modified? I'm not sure. At the moment, I'm not modifying anything in the class, just loading, defrosting it (?) and saving it, by using the code:
CtClass webViewClass = ClassPool.getDefault().get("javafx.scene.web.WebView");
webViewClass.defrost();
webViewClass.toClass();
This is my minimum reproducible example. First, Main.java:
package sample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javassist.CannotCompileException;
import javassist.ClassPool;
import javassist.CtClass;
import javassist.NotFoundException;
public class Main extends Application {
public static void main(String[] args) {
try {
CtClass webViewClass = ClassPool.getDefault().get("javafx.scene.web.WebView");
webViewClass.defrost();
webViewClass.toClass();
} catch (NotFoundException | CannotCompileException e) {
e.printStackTrace();
}
launch(args);
}
#Override
public void start(Stage primaryStage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
primaryStage.setScene(new Scene(root, 800, 600));
primaryStage.show();
}
}
and these are the contents of the Controller.java file:
package sample;
import javafx.fxml.FXML;
import javafx.scene.web.WebView;
public class Controller {
#FXML
private WebView webView;
#FXML
private void initialize() {
webView.getEngine().load("http://stackoverflow.com");
}
}
and this is the view, sample.fxml:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.web.WebView?>
<AnchorPane xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1"
fx:controller="sample.Controller">
<WebView fx:id="webView" minHeight="-Infinity" minWidth="-Infinity"/>
</AnchorPane>
The full exception is:
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$154(LauncherImpl.java:182)
at java.lang.Thread.run(Thread.java:748)
Caused by: javafx.fxml.LoadException:
/C:/Users/pupeno/Documents/Dashman/code/experiments/webviewwoes/out/production/webviewwoes/sample/sample.fxml:8
at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2579)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214)
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:27)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$161(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$174(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(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$147(WinApplication.java:177)
... 1 more
Caused by: java.lang.IllegalArgumentException: Can not set javafx.scene.web.WebView field sample.Controller.webView to javafx.scene.web.WebView
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:167)
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:171)
at sun.reflect.UnsafeObjectFieldAccessorImpl.set(UnsafeObjectFieldAccessorImpl.java:81)
at java.lang.reflect.Field.set(Field.java:764)
at javafx.fxml.FXMLLoader.injectFields(FXMLLoader.java:1163)
at javafx.fxml.FXMLLoader.access$1600(FXMLLoader.java:103)
at javafx.fxml.FXMLLoader$ValueElement.processValue(FXMLLoader.java:857)
at javafx.fxml.FXMLLoader$ValueElement.processEndElement(FXMLLoader.java:765)
at javafx.fxml.FXMLLoader.processEndElement(FXMLLoader.java:2823)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2532)
... 17 more
Exception running application sample.Main
Process finished with exit code 1
Most likely there are two versions of your Webview loaded by two different ClassLoaders, at least I would expect such an exception only in that case.
Note this paragraph from the javassist tutorial:
If the program is running on some application server such as JBoss and
Tomcat, the context class loader used by toClass() might be
inappropriate. In this case, you would see an unexpected
ClassCastException. To avoid this exception, you must explicitly give
an appropriate class loader to toClass(). For example, if bean is your
session bean object, then the following code:
CtClass cc = ...; Class c =
cc.toClass(bean.getClass().getClassLoader()); would work. You should
give toClass() the class loader that has loaded your program (in the
above example, the class of the bean object).
Calling toClass() makes the context class loader of the thread load the class. I assume when the FXMLLoader loads the view, it loads the classes with a different classloader, and somehow things get mixed up so that you end up with this exception. Another possible outcome would have been that everything loads fine, but your modifications don't work because the FXMLLoader loads unmodified classes.
Do something like this:
ClassPool classPool = ClassPool.getDefault();
CtClass webViewClass = classPool.get("javafx.scene.web.WebView");
webViewClass.defrost();
classPool.toClass(webViewClass, FXMLLoader.class.getClassLoader(), null);