I'm working on a gui app, using javafx. I tried running the Main app, but I keep getting an error.
Exception in Application start method
java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:118)
at java.base/java.lang.reflect.Method.invoke(Method.java:580)
at [email protected]/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:464)
at [email protected]/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:364)
at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:103)
at java.base/java.lang.reflect.Method.invoke(Method.java:580)
at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1135)
Caused by: java.lang.RuntimeException: Exception in Application start method
at [email protected]/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:893)
at [email protected]/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:196)
at java.base/java.lang.Thread.run(Thread.java:1583)
Caused by: javafx.fxml.LoadException:
/Users/mac/Documents/YK_Project/MusicalBookingTicketApp/bin/musicalticketbooking/musicals.fxml
at [email protected]/javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2722)
at [email protected]/javafx.fxml.FXMLLoader.importClass(FXMLLoader.java:2994)
at [email protected]/javafx.fxml.FXMLLoader.processImport(FXMLLoader.java:2838)
at [email protected]/javafx.fxml.FXMLLoader.processProcessingInstruction(FXMLLoader.java:2773)
at [email protected]/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2639)
at [email protected]/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2563)
at [email protected]/javafx.fxml.FXMLLoader.load(FXMLLoader.java:2531)
at musicalticketbooking.Main.start(Main.java:14)
at [email protected]/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:839)
at [email protected]/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:483)
at [email protected]/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:456)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:400)
at [email protected]/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:455)
at [email protected]/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
Caused by: java.lang.ClassNotFoundException: javafx.scene.control.HBox
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:526)
at [email protected]/javafx.fxml.FXMLLoader.loadTypeForPackage(FXMLLoader.java:3062)
at [email protected]/javafx.fxml.FXMLLoader.loadType(FXMLLoader.java:3051)
at [email protected]/javafx.fxml.FXMLLoader.importClass(FXMLLoader.java:2992)
... 12 more
Exception running application musicalticketbooking.Main
I'm using java 21.0.1 2023-10-17 LTS and javafx-sdk-21.0.1 and using vscode as my IDE
Here's what I have so far:
Main.java:
package musicalticketbooking;
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 stage) throws Exception {
FXMLLoader loader = new FXMLLoader(getClass().getResource("musicals.fxml"));
Parent root = loader.load();
Scene scene = new Scene(root);
// Set the controller for the FXML file
Controller controller = loader.getController();
stage.setTitle("Musical Ticket Booking System");
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Controller.java
package musicalticketbooking;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
public class Controller {
@FXML
private VBox musicalLists;
@FXML
private VBox schedules;
@FXML
private VBox formTicket;
@FXML
private Button bookButton;
@FXML
private void handleMusicals() {
}
@FXML
private void handleSchedule() {
}
@FXML
private void handleExit() {
}
@FXML
private void handleBook() {
}
}
musicals.fxml:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.HBox?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.VBox?>
<VBox xmlns="http://javafx.com/javafx"
xmlns:fx="http://javafx.com/fxml"
fx:controller="musicalticketbooking.Controller">
<Label text="Musical Ticket Booking System"/>
<HBox spacing="10">
<Button text="Musical List" onAction="#handleMusicals"/>
<Button text="Schedule List" onAction="#handleSchedule"/>
<Button text="Exit" onAction="#handleExit"/>
</HBox>
<VBox id="data-container">
<!-- Musical List -->
<VBox id="musical-lists">
<!-- Populate dynamically from Java code -->
</VBox>
<!-- Schedule List -->
<VBox id="schedules">
<!-- Populate dynamically from Java code -->
</VBox>
<VBox id="form-ticket">
<!-- Populate dynamically from Java code -->
</VBox>
</VBox>
<Button text="Book" onAction="#handleBook"/>
</VBox>
And my lunch.json:
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "java",
"name": "Musical Ticket Booking App",
"request": "launch",
"mainClass": "musicalticketbooking.Main",
"vmArgs": "--module-path /Users/mac/Downloads/javafx-sdk-21.0.1/lib --add-modules javafx.controls,javafx.fxml"
}
]
}
All files are in the same package musicalticketbooking;
You have to change the import of the fxml file (
musicals.fxml
) fromto: