LoadException on adding a controller to fxml file in Java 8 [duplicate] - java

This question already has answers here:
Why I'm getting javafx.fxml.LoadException even the path of the fxml file is correct
(2 answers)
Closed 4 years ago.
Every time i add a controller to the fxml file, i get an exception: javafx.fxml.LoadException, and when i print the exception message it shows the right directory to the file
I am using netbeans 8 IDE, Java 8 and Scene builder for the GUI
FXML:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ListView?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.text.Text?>
<AnchorPane id="AnchorPane" prefHeight="419.0" prefWidth="875.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="organizer.Controller.DeleteCategoryController">
<children>
<Pane layoutX="9.0" layoutY="108.0" prefHeight="283.0" prefWidth="550.0" style="-fx-border-width: 1pt; -fx-border-color: black;">
<children>
<Text fx:id="textDeleteThen" layoutX="195.0" layoutY="27.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Delete ... category then" />
<Button layoutX="14.0" layoutY="166.0" mnemonicParsing="false" prefHeight="103.0" prefWidth="241.0" text="Option 3
Create a new Relationship
and move contacts having .....
relationship to this relationship" />
<Button layoutX="266.0" layoutY="49.0" mnemonicParsing="false" prefHeight="103.0" prefWidth="241.0" text="Option 2
Keep all contacts
with the .... relationship" />
</children></Pane>
<Pane layoutX="569.0" layoutY="19.0" prefHeight="372.0" prefWidth="272.0" style="-fx-border-width: 1pt; -fx-border-color: black;" />
<Text layoutX="41.0" layoutY="73.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Choose one of the following options" wrappingWidth="447.681640625" />
<Text fx:id="textDeletingCategory" layoutX="42.0" layoutY="24.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Deleting ..." />
<Text fx:id="textMessage" layoutX="42.0" layoutY="48.0" strokeType="OUTSIDE" strokeWidth="0.0" text="After deleting .... do the following:" />
<Button alignment="CENTER" contentDisplay="CENTER" layoutX="279.0" layoutY="271.0" mnemonicParsing="false" prefHeight="103.0" prefWidth="241.0" text="Option 4
Change Contacts having
.... relationship
to the following
existing relationship" />
<Button layoutX="23.0" layoutY="153.0" mnemonicParsing="false" prefHeight="103.0" prefWidth="241.0" text="Option 1
Delete all contacts
with the ..... relationship" />
<TextField layoutX="585.0" layoutY="56.0" prefHeight="25.0" prefWidth="184.0" />
<ListView fx:id="listViewRemainingCategories" layoutX="580.0" layoutY="138.0" prefHeight="236.0" prefWidth="241.0" />
<Text layoutX="580.0" layoutY="115.0" strokeType="OUTSIDE" strokeWidth="0.0" text="List of other categories (for option 4)" />
<Text layoutX="585.0" layoutY="39.0" strokeType="OUTSIDE" strokeWidth="0.0" text="New Category Name (for option 3)" />
</children>
</AnchorPane>
And Controller:
package organizer.Controller;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.ListView;
import javafx.scene.text.Text;
import organizer.Model.SQL_Objects;
public class DeleteCategoryController extends SQL_Objects implements Initializable{
#FXML private ListView listViewRemainingCategories;
#FXML private Text textDeletingCategory;
#FXML private Text textMessage;
#FXML private Text textDeleteThen;
private int toRemoveId;
private String toRemoveName;
private int associatedRecordsCount;
public DeleteCategoryController(String toRemoveName, int toRemoveId, int associatedRecordCount) {
this.toRemoveName = toRemoveName;
this.toRemoveId = toRemoveId;
this.associatedRecordsCount = associatedRecordCount;
}
#Override
public void initialize(URL location, ResourceBundle resources) {
}
all other FXML files in my program work well, it is just this FXML file, and no matter which controller i add to it, it prevents it from loading, when i remove the controller attribute it opens normally.
Full stack trace:
javafx.fxml.LoadException:
file:/D:/Personal/Projects/Tools/Java/Organizer/dist/run798015614/Organizer.jar!/organizer/View/DeleteCategory.fxml:10
at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
at javafx.fxml.FXMLLoader.access$700(FXMLLoader.java:103)
at javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:932)
at javafx.fxml.FXMLLoader$InstanceDeclarationElement.processAttribute(FXMLLoader.java:971)
at javafx.fxml.FXMLLoader$Element.processStartElement(FXMLLoader.java:220)
at javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:744)
at javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2707)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2527)
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 organizer.Controller.Main.switchWindow(Main.java:32)
at organizer.Controller.Main.start(Main.java:21)
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)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.InstantiationException: organizer.Controller.DeleteCategoryController
at java.lang.Class.newInstance(Class.java:427)
at sun.reflect.misc.ReflectUtil.newInstance(ReflectUtil.java:51)
at javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:927)
... 23 more
Caused by: java.lang.NoSuchMethodException: organizer.Controller.DeleteCategoryController.<init>()
at java.lang.Class.getConstructor0(Class.java:3082)
at java.lang.Class.newInstance(Class.java:412)
... 25 more
Any idea why ?
I appreciate your help

So i found the solution, i will post here so someone might make use of that
When i removed the constructor the program worked, looks like in the JavaFx applications you can only have the default constructor, however if you need to add parameters to the constructor you then need to use what is known as ControllerFactory.

Related

JavaFX: Exception in Application Start Method java.lang.reflect.InvocationTargetException

I am literally desperate.
I don’t know why but every time I try to run my program this error comes out.
I am using Netbeans, with Java 10.0.2 in order to have already installed JavaFX.
I think it’s due to some problem with the FXML file.
Can you help me?
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:473)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:372)
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:941)
Caused by: java.lang.RuntimeException: Exception in Application start method
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:973)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:198)
at java.base/java.lang.Thread.run(Thread.java:844)
Caused by: javafx.fxml.LoadException: Root hasn't been set. Use method setRoot() before load.
file:/C:/Users/User/Documents/NetBeansProjects/gruppo71/dist/run2124011701/gruppo71.jar!/gruppo71/FXMLDocument.fxml:13
at javafx.fxml/javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2621)
at javafx.fxml/javafx.fxml.FXMLLoader.access$100(FXMLLoader.java:105)
at javafx.fxml/javafx.fxml.FXMLLoader$RootElement.constructValue(FXMLLoader.java:1338)
at javafx.fxml/javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:754)
at javafx.fxml/javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2722)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2552)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2466)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3253)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3210)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3179)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3152)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3129)
at javafx.fxml/javafx.fxml.FXMLLoader.load(FXMLLoader.java:3122)
at gruppo71.Gruppo71.start(Gruppo71.java:21)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:919)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$11(PlatformImpl.java:449)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$9(PlatformImpl.java:418)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:417)
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:175)
... 1 more
Exception running application gruppo71.Gruppo71
Java Result: 1
My main is this:
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
/**
*
* #author User
*/
public class Gruppo71 extends Application {
#Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.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 code is:
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ContextMenu?>
<?import javafx.scene.control.DatePicker?>
<?import javafx.scene.control.MenuItem?>
<?import javafx.scene.control.SplitPane?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<fx:root id="AnchorPane" prefHeight="300.0" prefWidth="450.0" type="AnchorPane" xmlns="http://javafx.com/javafx/10.0.2" xmlns:fx="http://javafx.com/fxml/1" fx:controller="gruppo71.FXMLDocumentController">
<children>
<SplitPane dividerPositions="0.13087248322147652" layoutX="28.0" orientation="VERTICAL" prefHeight="200.0" prefWidth="320.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<items>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="100.0" prefWidth="160.0">
<children>
<Button fx:id="aggiungiButton" layoutX="10.0" layoutY="5.0" mnemonicParsing="false" onAction="#addEvento" text="Aggiungi Evento" AnchorPane.bottomAnchor="5.0" AnchorPane.leftAnchor="10.0" AnchorPane.topAnchor="5.0" />
<DatePicker fx:id="dataPicker" layoutX="114.0" layoutY="6.0" prefHeight="26.0" prefWidth="150.0" AnchorPane.bottomAnchor="5.0" AnchorPane.leftAnchor="124.0" AnchorPane.topAnchor="5.0" />
<TextField fx:id="descriptionField" layoutX="285.0" layoutY="6.0" text="aggiungi descrizione..." AnchorPane.bottomAnchor="5.0" AnchorPane.leftAnchor="284.0" AnchorPane.rightAnchor="10.0" AnchorPane.topAnchor="5.0" />
</children>
</AnchorPane>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="100.0" prefWidth="160.0">
<children>
<TableView fx:id="mainTab" layoutX="14.0" prefHeight="256.0" prefWidth="448.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<columns>
<TableColumn fx:id="dataColumn" prefWidth="136.0" text="Data" />
<TableColumn fx:id="eventColumn" prefWidth="311.0" text="Evento" />
</columns>
<contextMenu>
<ContextMenu>
<items>
<MenuItem fx:id="removeButton" mnemonicParsing="false" onAction="#removeEvent" text="Rimuovi Evento" />
<MenuItem fx:id="importaButton" mnemonicParsing="false" onAction="#importCSV" text="Importa CSV" />
<MenuItem fx:id="exportButton" mnemonicParsing="false" onAction="#exportCSV" text="Esporta CSV" />
</items>
</ContextMenu>
</contextMenu>
</TableView>
</children>
</AnchorPane>
</items>
</SplitPane>
</children>
</fx:root>
All help would be greatly appreciated!!!
Just to mark this as answered: See #James_D' comment for the solution.
Replace <fx:root> in the FXML file with <AnchorPane> or set the root on the FXMLLoader before calling load() for example like this:
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("FXMLDocument.fxml"));
fxmlLoader.setRoot(new AnchorPane());
Parent root = fxmlLoader.load();
See also:
JavaFx FXML load file issues with setting root

Java: Exception in Application Start Method java.lang.reflect.InvocationTargetException [duplicate]

This question already has answers here:
IntelliJ can't recognize JavaFX 11 with OpenJDK 11
(7 answers)
Closed 2 years ago.
I have been trying to work on a Simon Says project, but I keep getting errors when I run it. I am using IntelliJ, with Java 13.0.2 and the latest version of JavaFX.
The error is:
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:567)
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:567)
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:830)
Caused by: java.lang.IllegalAccessError: class com.sun.javafx.fxml.FXMLLoaderHelper (in unnamed module #0x229b412d) 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 #0x229b412d
at com.sun.javafx.fxml.FXMLLoaderHelper.<clinit>(FXMLLoaderHelper.java:38)
at javafx.fxml.FXMLLoader.<clinit>(FXMLLoader.java:2056)
at Main.start(Main.java:15)
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 Main
My main is this:
import javafx.application.Application;
import javafx.fxml.*;
import javafx.scene.*;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
public class Main extends Application {
private Stage primaryStage;
private AnchorPane rootLayout;
public void start(Stage primaryStage) {
try {
this.primaryStage = primaryStage;
primaryStage.setTitle("Simon Game - java ripoff edition");
FXMLLoader loader = new FXMLLoader(Main.class.getResource("GUI.fxml"));
Parent root = loader.load();
Scene scene = new Scene(this.rootLayout);
primaryStage.setScene(scene);
primaryStage.show();
scene.getStylesheets().add(this.getClass().getResource("application.css").toExternalForm());
} catch (Exception var4) {
var4.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
and my FXML code is:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.TabPane?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.shape.Rectangle?>
<?import javafx.scene.text.Font?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" fx:1d="root" prefHeight="400.0" prefWidth="600.0" style="-fx-background-color: silver;" xmlns="http://javafx.com/javafx/8.0.60" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.Controller">
<children>
<Button fx:id="startBtn" layoutX="466.0" layoutY="140.0" mnemonicParsing="false" onAction="#handleButtonAction" prefHeight="47.0" prefWidth="99.0" text="Start">
<font>
<Font name="Gill Sans MT" size="19.0" />
</font>
</Button>
<Rectangle fx:id="greenBtn" arcHeight="5.0" arcWidth="5.0" fill="#0ab916" height="150.0" layoutX="121.0" layoutY="51.0" onMouseClicked="#userInput" stroke="BLACK" strokeType="INSIDE" width="150.0" />
<Rectangle fx:id="redBtn" arcHeight="5.0" arcWidth="5.0" fill="#ff1f1f" height="150.0" layoutX="271.0" layoutY="51.0" onMouseClicked="#userInput" stroke="BLACK" strokeType="INSIDE" width="150.0" />
<Rectangle fx:id="yellowbtn" arcHeight="5.0" arcWidth="5.0" fill="#e8eb23" height="150.0" layoutX="121.0" layoutY="201.0" onMouseClicked="#userInput" stroke="BLACK" strokeType="INSIDE" width="150.0" />
<Rectangle fx:id="blueBtn" arcHeight="5.0" arcWidth="5.0" fill="DODGERBLUE" height="150.0" layoutX="271.0" layoutY="201.0" onMouseClicked="#userInput" stroke="BLACK" strokeType="INSIDE" width="150.0" />
<TabPane layoutX="37.0" prefHeight="200.0" prefWidth="200.0" tabClosingPolicy="UNAVAILABLE" />
<Button fx:id="instructBtn" layoutX="466.0" layoutY="208.0" mnemonicParsing="false" onAction="#handleButtonAction" prefHeight="47.0" prefWidth="99.0" text="Instructions">
<font>
<Font name="Gill Sans MT" size="16.0" />
</font>
</Button>
</children>
</AnchorPane>
I have other classes that work the buttons, but I'm only getting the error from the Main class. All help would be greatly appreciated!
You need to import all the required packages.

When I try to load a FXML file I get a java.io.IOException error [duplicate]

This question already has answers here:
JavaFx, Problems with #FXML
(2 answers)
Closed 3 years ago.
I am trying to load an fxml file I built with scene builder but for some reason, I get a java.io.IOException error.
Here is the error I get:
/Library/Java/JavaVirtualMachines/jdk-12.jdk/Contents/Home/bin/java --module-path /Users/anish/codingprojects/javafx-sdk-11.0.2/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:/Applications/IntelliJ IDEA CE.app/Contents/lib/idea_rt.jar=53528:/Applications/IntelliJ IDEA CE.app/Contents/bin" -Dfile.encoding=UTF-8 -classpath /Users/anish/IdeaProjects/FBLA/out/production/FBLA:/Users/anish/codingprojects/javafx-sdk-11.0.2/lib/javafx-swt.jar:/Users/anish/codingprojects/javafx-sdk-11.0.2/lib/javafx.base.jar:/Users/anish/codingprojects/javafx-sdk-11.0.2/lib/javafx.controls.jar:/Users/anish/codingprojects/javafx-sdk-11.0.2/lib/javafx.fxml.jar:/Users/anish/codingprojects/javafx-sdk-11.0.2/lib/javafx.graphics.jar:/Users/anish/codingprojects/javafx-sdk-11.0.2/lib/javafx.media.jar:/Users/anish/codingprojects/javafx-sdk-11.0.2/lib/javafx.swing.jar:/Users/anish/codingprojects/javafx-sdk-11.0.2/lib/javafx.web.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:567)
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:567)
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:835)
Caused by: javafx.fxml.LoadException:
/Users/anish/IdeaProjects/FBLA/out/production/FBLA/sample/mainpage.fxml:35
at javafx.fxml/javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2625)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2603)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2466)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3237)
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 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(AccessController.java:389)
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)
Caused by: java.lang.IllegalArgumentException: Can not set javax.swing.text.html.ListView field sample.MainPageController.hoursLog to javafx.scene.control.ListView
at java.base/jdk.internal.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:167)
at java.base/jdk.internal.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:171)
at java.base/jdk.internal.reflect.UnsafeObjectFieldAccessorImpl.set(UnsafeObjectFieldAccessorImpl.java:81)
at java.base/java.lang.reflect.Field.set(Field.java:780)
at javafx.fxml/javafx.fxml.FXMLLoader.injectFields(FXMLLoader.java:1174)
at javafx.fxml/javafx.fxml.FXMLLoader.access$1600(FXMLLoader.java:105)
at javafx.fxml/javafx.fxml.FXMLLoader$ValueElement.processValue(FXMLLoader.java:865)
at javafx.fxml/javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:759)
at javafx.fxml/javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2722)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2552)
... 14 more
Exception running application sample.Main
Here is my main class code:
package sample;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import java.io.IOException;
public class addHours {
#FXML
public void display()
{
try {
Pane addHoursPane = FXMLLoader.load(getClass().getResource("addHours.fxml"));
} catch (IOException e) {
e.printStackTrace();
}
}
}
Here is my FXML file that I built using scene builder:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ListView?>
<?import javafx.scene.control.Menu?>
<?import javafx.scene.control.MenuBar?>
<?import javafx.scene.control.MenuItem?>
<?import javafx.scene.control.SplitPane?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.TextFlow?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="518.0" prefWidth="704.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.MainPageController">
<children>
<MenuBar AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<menus>
<Menu mnemonicParsing="false" text="File">
<items>
<MenuItem mnemonicParsing="false" text="Close" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Edit">
<items>
<MenuItem mnemonicParsing="false" text="Delete" />
<MenuItem mnemonicParsing="false" text="Unspecified Action" />
</items>
</Menu>
</menus>
</MenuBar>
<SplitPane fx:id="splitPane" dividerPositions="0.23062015503875968" orientation="VERTICAL" prefHeight="518.0" prefWidth="704.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="155.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="28.0">
<items>
<AnchorPane maxHeight="300.0" minHeight="100.0" prefHeight="100.0" prefWidth="160.0" />
<AnchorPane prefHeight="100.0" prefWidth="160.0">
<children>
<ListView fx:id="hoursLog" layoutX="136.0" layoutY="65.0" prefHeight="393.0" prefWidth="527.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" />
</children>
</AnchorPane>
</items>
</SplitPane>
<Button fx:id="addHoursButton" layoutX="24.0" layoutY="329.0" mnemonicParsing="false" prefHeight="35.0" prefWidth="115.0" text="Add Hours" AnchorPane.bottomAnchor="142.0" />
<Button fx:id="settings" layoutX="24.0" layoutY="426.0" mnemonicParsing="false" prefHeight="35.0" prefWidth="115.0" text="Settings" AnchorPane.bottomAnchor="82.0" />
<ImageView fx:id="profilePicture" fitHeight="45.0" fitWidth="45.0" layoutX="1.0" layoutY="29.0" pickOnBounds="true" preserveRatio="true" AnchorPane.leftAnchor="2.0" AnchorPane.topAnchor="30.0" />
<TextFlow fx:id="profileDetails" layoutX="2.0" layoutY="75.0" prefHeight="35.0" prefWidth="101.0" AnchorPane.leftAnchor="2.0" AnchorPane.topAnchor="77.0" />
</children>
</AnchorPane>
And here is my project structure
(Never made a resources tab because then I could not connect my controller with my fxml files)
I'm just starting out with JavaFX so I am probably not seeing something common here but I have researched everywhere online and nothing seems to fix it.
Btw I am using JavaFX 11.02, Java SDK 12 and IntelliJ IDE
You are importing the wrong ListView.
Look at the line with your actual Exception:
IllegalArgumentException: Can not set javax.swing.text.html.ListView field sample.MainPageController.hoursLog to javafx.scene.control.ListView
Usually this means your FXML is defining a JavaFX control, but your actual class is importing javax.swing.text.html.ListView instead of javafx.scene.control.ListView.
Your question does not include MainPageController.java, where this error is coming from, so I cannot answer definitively, but I'd look there.
Side Note: I do recommend learning the Java naming conventions and sticking to them in the future.

FXML Contorller error in jar: at javafx.fxml.FXMLLoader.constructLoadException

I am using the Maven Shade plugin with Eclipse and JavaFX. Maven Shade creates a runnable jar to allow me to run my program. The program compiles and runs and shows the GUI with no errors via eclipse. The maven build runs with no errors and create the jar, the issue comes when I try to run my jar.
I get the following error.
I have read a few similar issues on here but none of them seemed to have the same setup as myself. Similar This guy moved the fxml files into his resources folder but I wanted to try keep my project MVC. Is there another fix?
My issue seems to be the fx:controller= in the MainMenu.fxml
Error: Caused by: java.lang.IllegalStateException: Location is not set.
ERROR
javafx.fxml.LoadException:
file:/Users/bob/git/Corkscrew/Corkscrew/Corkscrew%20Server/target/Corkscrew-0.0.1-SNAPSHOT.jar!/io/ironbytes/corkscrew/views/MainMenu.fxml:10
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 io.ironbytes.corkscrew.models.MainMenu.start(MainMenu.java:23)
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)
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 io.ironbytes.corkscrew.models.ConnectionsPane.<init>(ConnectionsPane.java:33)
at io.ironbytes.corkscrew.controllers.MainMenuController.<init>(MainMenuController.java:32)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at java.lang.Class.newInstance(Class.java:442)
at sun.reflect.misc.ReflectUtil.newInstance(ReflectUtil.java:51)
at javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:927)
at javafx.fxml.FXMLLoader$InstanceDeclarationElement.processAttribute(FXMLLoader.java:971)
at javafx.fxml.FXMLLoader$Element.processStartElement(FXMLLoader.java:220)
at javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:744)
at javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2707)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2527)
... 14 more
MainMenu.java
package io.ironbytes.corkscrew.models;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class MainMenu extends Application {
Parent root;
Stage stage;
#Override
public void start(Stage primaryStage) {
try {
root = FXMLLoader.load(getClass().getClassLoader().getResource("io/ironbytes/corkscrew/views/MainMenu.fxml"));
stage = primaryStage;
stage.setTitle("Welcome");
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
MainMenu.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.control.ToolBar?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.BorderPane?>
<BorderPane fx:id="mainPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="900.0" xmlns="http://javafx.com/javafx/9" xmlns:fx="http://javafx.com/fxml/1" fx:controller="io.ironbytes.corkscrew.controllers.MainMenuController">
<left>
<ToolBar orientation="VERTICAL" BorderPane.alignment="CENTER">
<items>
<Button mnemonicParsing="false" onAction="#onConnectionsBtnClick" prefHeight="27.0" prefWidth="82.0" text="Clients" />
<Button mnemonicParsing="false" onAction="#onBuilderBtnClick" prefHeight="27.0" prefWidth="83.0" text="Builder" />
<Button mnemonicParsing="false" prefHeight="27.0" prefWidth="83.0" text="Crypter" />
<Button mnemonicParsing="false" onAction="#onCorkscrewBtnClick" text="Corkscrew" />
<Button fx:id="listenBtn" mnemonicParsing="false" onAction="#onListenBtnClick" prefHeight="27.0" prefWidth="82.0" text="Listen" textFill="#e43333" />
<TextField prefHeight="27.0" prefWidth="8.0" promptText="Port: 1337" />
</items>
</ToolBar>
</left>
<center>
<ImageView fitHeight="255.0" fitWidth="348.0" pickOnBounds="true" preserveRatio="true" BorderPane.alignment="CENTER">
<image>
<Image url="#../../../../img/logo.png" />
</image>
</ImageView>
</center>
</BorderPane>
I finally found a solution. After hours debugging, playing with pom.xml files and changing paths and file structures, I realized the issue was was the jar could not load or does not like the fx:controller located in the fxml file. So I needed to load the controller and link it to the fxml file without using fx:controller=
Solution:
Remove fx:contorller="io.ironbytes.corkscrew.view.MainMenu.fxml"
and change how you load the contorller:
try {
FXMLLoader loader = new FXMLLoader();
loader.setController(new MainMenuController());
loader.setLocation(getClass().getResource("/io/ironbytes/corkscrew/views/MainMenu.fxml"));
root = loader.load();
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.show();
} catch (Exception e) {
e.printStackTrace();
}
}`

Netbeans says class does not exist in FXML file for observablearrayList but it does

I have this FXML file generated by Netbeans (but I have modified the TableColumn bit):
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>
<?import javafx.scene.control.cell.*?>
<?import javafx.collections.*?>
<AnchorPane id="AnchorPane" fx:id="MainPane" prefHeight="529.0" prefWidth="513.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2" fx:controller="d1example2.UserInterfaceController">
<children>
<SplitPane dividerPositions="0.17835671342685372" focusTraversable="true" layoutX="14.0" layoutY="14.0" orientation="VERTICAL" prefHeight="501.0" prefWidth="485.0">
<items>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="100.0" prefWidth="160.0">
<children>
<AnchorPane id="AnchorPane" layoutX="95.0" layoutY="32.0">
<children>
<TextField fx:id="FirstField" layoutX="0.0" layoutY="0.0" prefWidth="51.0" />
<Text fx:id="TimesText" layoutX="65.0" layoutY="16.0" strokeType="OUTSIDE" strokeWidth="0.0" text="*" />
<TextField id="FirstField" layoutX="90.0" layoutY="0.0" prefWidth="51.0" />
<Text fx:id="EqualSign" layoutX="168.0" layoutY="16.0" strokeType="OUTSIDE" strokeWidth="0.0" text="=" />
<Text fx:id="EquationResult" layoutX="245.0" layoutY="16.0" scaleX="1.632154219166214E8" scaleY="1.436917974231407" strokeType="OUTSIDE" strokeWidth="0.0" text="" />
</children>
</AnchorPane>
<Button fx:id="SubmitButton" layoutX="356.0" layoutY="32.0" mnemonicParsing="false" onMouseClicked="#handleButtonAction" text="Submit" />
</children>
</AnchorPane>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="100.0" prefWidth="160.0">
<children>
<TableView layoutX="13.0" layoutY="14.0" prefHeight="379.0" prefWidth="456.0">
<columns>
<TableColumn text="Half Value">
<cellValueFactory><PropertyValueFactory property="HalfContent" />
</cellValueFactory>
</TableColumn>
<TableColumn text="Double Value">
<cellValueFactory><PropertyValueFactory property="DoubleContent" />
</cellValueFactory>
</TableColumn>
</columns>
<items>
<FXCollections fx:factory="observableArrayList">
<Data HalfContent="First Column Text" DoubleContent="Second Column Text" />
</FXCollections>
</items>
</TableView>
</children>
</AnchorPane>
</items>
</SplitPane>
</children>
</AnchorPane>
And this other class called "Data" which looks like this:
package d1example2;
import javafx.beans.property.SimpleStringProperty;
/**
*
* #author zmeshign
*/
public final class Data {
private final SimpleStringProperty HalfContent = new SimpleStringProperty("");
private final SimpleStringProperty DoubleContent = new SimpleStringProperty("");
public Data() {
this("", "");
}
public Data(String HalfContent, String DoubleContent) {
setHalfContent(HalfContent);
setDoubleContent(DoubleContent);
}
public String getHalfContent() {
return HalfContent.get();
}
public void setHalfContent(String hContent) {
HalfContent.set(hContent);
}
public String getDoubleContent() {
return DoubleContent.get();
}
public void setDoubleContent(String dContent) {
DoubleContent.set(dContent);
}
}
So in this line:
<Data HalfContent="First Column Text" DoubleContent="Second Column Text" />
it says that the class "Data" does not exist while I have it under the same package. All I'm trying to do is to learn how to display a string under a column in a TableView.
I would immensely appreciate any help!
Thanks
How to fix it
In your case the appropriate directive you need to add to the FXML file in order for it to find your Data class is:
<?import d1example2.Data?>
Why it was failing for you
FXML files are just files, they are not compiled to Java classes (at least not in the JavaFX 2.2 version). This means that the files themselves aren't really aware of what package they are in (as packaging is a Java concept). The FXML files can be told where to locate the Java classes they need though. To do this you use the <?import java.lang.*?> directive at the top of the file (substituting your package name to perform the appropriate import).
Update based on additional question
This fixed the problem with the "data" part, but now it's saying: Class d1example2.Data does not support property "HalfContent". It also says this with "DoubleContent".
You need to follow Java property naming conventions. Under these conventions property names start with a lower case letter rather than an uppercase letter, so use halfContent and doubleContent as your property names in FXML.
Instead of:
<Data HalfContent="First Column Text" DoubleContent="Second Column Text" />
Write:
<Data halfContent="First Column Text" doubleContent="Second Column Text" />
Additionally, you might want to define property accessors for your properties:
public StringProperty halfContentProperty() {
return halfContent;
}
. . .

Categories