I'm having some issues getting a simple hello world application to run. It is throwing the following error:
Exception in Application start method
Exception in thread "main" java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$152(LauncherImpl.java:182)
at com.sun.javafx.application.LauncherImpl$$Lambda$2/1329552164.run(Unknown Source)
at java.lang.Thread.run(Thread.java:745)
Caused by: javafx.fxml.LoadException: Error resolving onAction='#printHello', either the event handler is not in the Namespace or there is an error in the script.
/home/willi/java/IdeaProjects/JavaFXApp/out/production/JavaFXApp/sample/sample.fxml:23
at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
at javafx.fxml.FXMLLoader.access$100(FXMLLoader.java:104)
at javafx.fxml.FXMLLoader$Element.processEventHandlerAttributes(FXMLLoader.java:606)
at javafx.fxml.FXMLLoader$ValueElement.processEndElement(FXMLLoader.java:766)
at javafx.fxml.FXMLLoader.processEndElement(FXMLLoader.java:2827)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2536)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2445)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3218)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3179)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3152)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3128)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3108)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3101)
at sample.Main.start(Main.java:13)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$159(LauncherImpl.java:863)
at com.sun.javafx.application.LauncherImpl$$Lambda$57/2085742749.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$172(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl$$Lambda$52/738441956.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$null$170(PlatformImpl.java:295)
at com.sun.javafx.application.PlatformImpl$$Lambda$55/689939224.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$171(PlatformImpl.java:294)
at com.sun.javafx.application.PlatformImpl$$Lambda$53/1382865734.run(Unknown Source)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
at com.sun.glass.ui.gtk.GtkApplication.lambda$null$48(GtkApplication.java:139)
at com.sun.glass.ui.gtk.GtkApplication$$Lambda$43/704249306.run(Unknown Source)
... 1 more
My Controller.java file has the methods printHello and printWorld defined in them, and I would like to call them when I press their corresponding buttons. The code is as follows:
package sample;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import java.awt.event.ActionEvent;
public class Controller {
#FXML
private Button helloButton;
#FXML
private Button worldButton;
#FXML
private void printHello(ActionEvent e){
System.out.println("Hello");
}
#FXML
private void printWorld(ActionEvent f){
System.out.println("World");
}
}
` In my fxml file, I have two buttons defined. Their code is as follows:
<Button fx:id="helloButton" layoutX="46.0" layoutY="87.0" mnemonicParsing="false" onAction="#printHello" text="Hello" />
<Button fx:id="worldButton" layoutX="97.0" layoutY="87.0" mnemonicParsing="false" onAction="#printWorld" text="World" />
Why is my code throwing this error?
Sorry in advance for the terrible formatting, this is my first time posting.
You have imported wrong ActionEvent in controller class.
import java.awt.event.ActionEvent;
Correct it to
import javafx.event.ActionEvent;
Related
This question already has an answer here:
How do I determine the correct path for FXML files, CSS files, Images, and other resources needed by my JavaFX Application?
(1 answer)
Closed 2 years ago.
I am trying to create a JavaFX program, and every time I try to run my code I am getting an exception - I'm not entirely sure what it means though ...
My code:
package com.PickingList.ui;
import java.io.IOException;
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 {
private Stage stage;
#Override
public void start(Stage primaryStage) throws IOException {
Scene scene;
FXMLLoader loader = new FXMLLoader(this.getClass().getResource("./TelaLogin.fxml"));
Parent root = loader.load();
stage.setScene(new Scene(root));
stage.setTitle("Login");
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
I'm using Eclipse to create the project, and I'm getting the following error:
Exception in Application start method
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
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(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.launcher.LauncherHelper$FXHelper.main(Unknown Source)
Caused by: java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$1(LauncherImpl.java:182)
at java.lang.Thread.run(Unknown Source)
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 com.PickingList.ui.Main.start(Main.java:19)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$8(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$7(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$5(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$6(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$4(WinApplication.java:186)
Exception running application com.PickingList.ui.Main
enter image description here
Try:
FXMLLoader loader = new FXMLLoader(this.getClass().getResource("/TelaLogin.fxml"));
Assuming that, your folder structure looks like:
/src/TelaLogin.fxml
I have some problems. I added GUI and I can't run my app. I was trying set sources in other way but it not helped.
Start class:
public class Main extends Application
{
#Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("gui/Main.fxml"));
Scene scene = new Scene(root);
stage.setMinHeight(600);
stage.setMinWidth(800);
stage.setScene(scene);
stage.show();
}
I was trying:
("/gui/Main.fxml");
("Main.fxml");
("/Main.fxml");
("gui/Main") etc.
Main class:
public static void main(String[] args) throws SQLException, IOException {
String databaseUrl = "jdbc:sqlite:bazadanych.db";
ConnectionSource connectionSource;
connectionSource = new JdbcConnectionSource(databaseUrl);
Dao<Klient, Integer> klientsDao =
DaoManager.createDao(connectionSource, Klient.class);
Dao<Samochod,Integer> samochodsDao =
DaoManager.createDao(connectionSource, Samochod.class);
Dao<Usterka,Integer> usterkasDao =
DaoManager.createDao(connectionSource, Usterka.class);
launch(args);
connectionSource.close();
}
First line of FXML file:
<AnchorPane minHeight="600.0" minWidth="800.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="gui/MainController">
Controller:
package gui;
public class MainController implements Initializable {
#Override
public void initialize(URL url, ResourceBundle rb) {
}
}
Error:
"C:\Program Files\Java\jdk1.8.0_161\bin\java.exe" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2018.3\lib\idea_rt.jar=55783:C:\Program Files\JetBrains\IntelliJ IDEA 2018.3\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_161\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\rt.jar;C:\Users\Kowali\Desktop\Projekt0-2\Projekt\target\classes;C:\Users\Kowali\.m2\repository\com\j256\ormlite\ormlite-jdbc\5.1\ormlite-jdbc-5.1.jar;C:\Users\Kowali\.m2\repository\com\j256\ormlite\ormlite-core\5.1\ormlite-core-5.1.jar;C:\Users\Kowali\.m2\repository\org\xerial\sqlite-jdbc\3.25.2\sqlite-jdbc-3.25.2.jar" Main
2018-12-04 16:10:59,239 [DEBUG] DaoManager created dao for class class Klient with reflection
2018-12-04 16:10:59,241 [DEBUG] DaoManager created dao for class class Samochod with reflection
2018-12-04 16:10:59,245 [DEBUG] DaoManager created dao for class class Usterka with reflection
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/Kowali/Desktop/Projekt0-2/Projekt/target/classes/Main.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:922)
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 Main.start(Main.java:18)
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.ClassNotFoundException: MainController
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:338)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:920)
... 22 more
Exception running application Main
Process finished with exit code 1
I'm working on Intelij so I moved FXML and Controller to resources but it not helped me too. I also read some responses on similar questions but they not helped me. I also trying to use FXML Loader, not Parent root. I dont understand too why in Intelij source to Controller is ok, but in teh end of error we can see its not found. I was trying to find the solution all day and nothin.
I was trying this too:
FXMLLoader loader = FXMLLoader.load(getClass().getResource("Main.fxml"));
Parent root = loader.load();
The file it is loading is C:/Users/Kowali/Desktop/Projekt0-2/Projekt/target/classes/Main.fxml. It may be an old file. I suggest you delete that file, do Project -> Rebuild project in IntelliJ, and try running it again.
Also, you claim that your code is
Parent root = FXMLLoader.load(getClass().getResource("gui/Main.fxml"));
but this is not consistent with the error message. So either you didn't save your .java file (but that's probably not the case since you're using IntelliJ, which saves files automatically), or this isn't the version of the code that matches the error message.
I'm working on a mp3 player which is using a JavaFX UI. When I start the application in NetBeans, it works fine. But when I create an executable jar with Clean and Build and start it with the cmd, I get an exception where the FXMLLoader loads the fxml file.
There must be a problem with the paths, right?
First Project Structure
Project Structure
I created a second application and stored everything in one folder. Created an executable jar and it worked fine.
Second Project Structure
Project Structure
Source Code (First)
package jornsfx;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class JornsFX extends Application{
#Override
public void start(Stage stage)throws Exception{
stage.setResizable(false);
Parent root = FXMLLoader.load(getClass().getResource("/views/jornsFX.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.setTitle("JornsFX");
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Exception (First)
Exception in Application start method
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
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(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.launcher.LauncherHelper$FXHelper.main(Unknown Source)
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(Unknown Source)
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 jornsfx.JornsFX.start(JornsFX.java:14)
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 jornsfx.JornsFX
I can't tell you for certain, but it looks like
getClass().getResource("/views/jornsFX.fxml")
in your start() method is null. The exception suggests it and also the file is named /views/JornsFX.fxml in the screenshot.
I have a ListView that contains "Task "objects. I want to be able to select a task from the ListView and get the values of that specific object. I am trying to do this by calling a method (startTask()) that gets the selected item in the ListView (a task object) and gets its values by using the getter methods in the task object. For the moment i am just printing the object to the console.
When I run the application this is the error that I get.
Exception in Application start method
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
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(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.launcher.LauncherHelper$FXHelper.main(Unknown Source)
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(Unknown Source)
Caused by: javafx.fxml.LoadException:
/C:/Users/Brian.Nora-PC/workspace/TaskApp/bin/application/taskapp.fxml:63
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 application.Main.start(Main.java:20)
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
Caused by: java.lang.IllegalArgumentException: Unable to coerce #startTask() to interface javafx.event.EventHandler.
at com.sun.javafx.fxml.BeanAdapter.coerce(BeanAdapter.java:496)
at com.sun.javafx.fxml.BeanAdapter.put(BeanAdapter.java:258)
at com.sun.javafx.fxml.BeanAdapter.put(BeanAdapter.java:54)
at javafx.fxml.FXMLLoader$Element.applyProperty(FXMLLoader.java:512)
at javafx.fxml.FXMLLoader$Element.processValue(FXMLLoader.java:363)
at javafx.fxml.FXMLLoader$Element.processPropertyAttribute(FXMLLoader.java:325)
at javafx.fxml.FXMLLoader$Element.processInstancePropertyAttributes(FXMLLoader.java:235)
at javafx.fxml.FXMLLoader$ValueElement.processEndElement(FXMLLoader.java:767)
at javafx.fxml.FXMLLoader.processEndElement(FXMLLoader.java:2823)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2532)
... 17 more
Exception running application application.Main
Here is the fxml file that contains the ListView and Button with startTask() method call when the button is clicked
<Tab text="Tasks">
<content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0">
<children>
<Label fx:id="noOfTasks" layoutX="403.0" layoutY="294.0" prefHeight="17.0" prefWidth="190.0" text="You have 0 tasks to be completed" />
<ListView fx:id="taskList" layoutX="8.0" layoutY="14.0" prefHeight="274.0" prefWidth="585.0" />
<Button layoutX="8.0" layoutY="299.0" mnemonicParsing="false" text="Start Task" OnAction="#startTask()"/>
</children>
</AnchorPane>
</content>
</Tab>
Here is the startTask() method
public void startTask(){
Task taskToStart = taskList.getSelectionModel().getSelectedItem();
System.out.println(taskToStart);
}
Here is my initialisation of ListView in Controller.java
public ListView<Task> taskList = new ListView<Task>();
Any help would be much appreciated thanks.
Specifying controller methods as event handlers requires you to use a # followed by the method's name and nothing else (see also Introduction to FXML - Controller Method Event Handlers ). Remove the () from the onAction attribute:
<Button layoutX="8.0" layoutY="299.0" mnemonicParsing="false" text="Start Task" onAction="#startTask"/>
Furthermore there should be no need to initialize the ListView using a constructor call in the code, if you inject it from the fxml; Furthermore a ListView created using a constructor call won't insert itself to the scene. It's likely that you just create a second ListView that is never shown. Instead there should be a ListView field with the name taskList in the controller class; this field should not be initialized from java code:
#FXML
private ListView<Task> taskList;
You can try something like this...
ObservableList<Node> list=taskList.getChildren();
for(int i=0;i<list.size();i++) {
Task task=list.get(i);
}
I'm very, VERY new to using JavaFX and FXML in general, and I've run into a bit of a problem that I've not been able to solve either through repeated Google searches, or searches here on Stack Exchange. While others have had similar problems, I haven't been able to replicate their solutions within my own project.
Right now, I'm mostly just trying to test JavaFX with FXML and get a feel for it... however I can't even get it to load, as FXMLLoader is giving me the following error.
javafx.fxml.LoadException:
/C:/Users/Dylon/workspace/Convergence_titanExplorationModule/bin/com/test/fxml/ExplorationModuleUI.fxml
at javafx.fxml.FXMLLoader.constructLoadException(Unknown Source) at
javafx.fxml.FXMLLoader.importClass(Unknown Source) at
javafx.fxml.FXMLLoader.processImport(Unknown Source) at
javafx.fxml.FXMLLoader.processProcessingInstruction(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source) at
javafx.fxml.FXMLLoader.loadImpl(Unknown Source) at
javafx.fxml.FXMLLoader.loadImpl(Unknown Source) at
javafx.fxml.FXMLLoader.loadImpl(Unknown Source) at
javafx.fxml.FXMLLoader.loadImpl(Unknown Source) at
javafx.fxml.FXMLLoader.loadImpl(Unknown Source) at
javafx.fxml.FXMLLoader.loadImpl(Unknown Source) at
javafx.fxml.FXMLLoader.load(Unknown Source) at
com.test.fxml.Main.start(Main.java:14) at
com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$163(Unknown
Source) at
com.sun.javafx.application.PlatformImpl.lambda$runAndWait$176(Unknown
Source) at
com.sun.javafx.application.PlatformImpl.lambda$null$174(Unknown
Source) at java.security.AccessController.doPrivileged(Native Method)
at
com.sun.javafx.application.PlatformImpl.lambda$runLater$175(Unknown
Source) at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(Unknown
Source) at com.sun.glass.ui.win.WinApplication._runLoop(Native
Method) at
com.sun.glass.ui.win.WinApplication.lambda$null$149(Unknown Source)
at java.lang.Thread.run(Unknown Source) Caused by:
java.lang.ClassNotFoundException at
javafx.fxml.FXMLLoader.loadType(Unknown Source) ... 21 more
Now, here's the code I'm working with...
package com.test.fxml;
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 {
#Override
public void start(Stage primaryStage) {
try {
Parent root = FXMLLoader.load(getClass().getResource("/com/test/fxml/ExplorationModuleUI.fxml"));
Scene scene = new Scene(root,400,400);
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
Here's my FXML file, named ExplorationModuleUI.fxml...
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.control*?>
<BorderPane xmlns:fx="http://javafx.com/fxml/1">
<TOP>
<HBox>
<Button text = "test"/>
</HBox>
</TOP>
</BorderPane>
Finally, here's how I have things laid out in regards to folders.
I can't post images yet so here's a link to one instead
Any help is greatly appreciated. I honestly haven't been able to figure out why it doesn't work even after digging about for a couple of hours tonight. I've tried other solutions I've found on here and in Google searches, but nothing has worked yet. If you have any questions feel free to ask and I'll get back to you as soon as I can in the morning.
First, your second import is missing a dot between control and *, it should be import javafx.scene.control.*
Second, TOP is not a valid element for fxml, use top instead (all lowercase).