(JavaFX) Exception in Application start method java.lang.reflect.InvocationTargetException

182 Views Asked by At

I am following a youtube video link I am trying to make a java program with a JavaFX interface and I am using FXML but i got this InvocationTargetException error.

i am using JDK 21, NetBeans 20.

Can anyone help me, please?

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 javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:464)
    at javafx.graphics/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 javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:893)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:196)
    at java.base/java.lang.Thread.run(Thread.java:1583)
Caused by: java.lang.NoClassDefFoundError: com/sun/javafx/css/parser/CSSParser
    at de.jensd.fx.glyphs.GlyphIcon.<clinit>(GlyphIcon.java:49)
    at java.base/jdk.internal.misc.Unsafe.ensureClassInitialized0(Native Method)
    at java.base/jdk.internal.misc.Unsafe.ensureClassInitialized(Unsafe.java:1160)
    at java.base/jdk.internal.reflect.MethodHandleAccessorFactory.ensureClassInitialized(MethodHandleAccessorFactory.java:300)
    at java.base/jdk.internal.reflect.MethodHandleAccessorFactory.newConstructorAccessor(MethodHandleAccessorFactory.java:103)
    at java.base/jdk.internal.reflect.ReflectionFactory.newConstructorAccessor(ReflectionFactory.java:200)
    at java.base/java.lang.reflect.Constructor.acquireConstructorAccessor(Constructor.java:549)
    at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:499)
    at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:486)
    at javafx.fxml/javafx.fxml.FXMLLoader$InstanceDeclarationElement.constructValue(FXMLLoader.java:1022)
    at javafx.fxml/javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:759)
    at javafx.fxml/javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2853)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2649)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2563)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3376)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3332)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3300)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3272)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3248)
    at javafx.fxml/javafx.fxml.FXMLLoader.load(FXMLLoader.java:3241)
    at bikerentalsystem1.BikeRentalSystem1.start(BikeRentalSystem1.java:24)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:839)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:483)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:456)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:400)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:455)
    at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    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:185)
    ... 1 more
Caused by: java.lang.ClassNotFoundException: com.sun.javafx.css.parser.CSSParser
    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)
    ... 30 more
Exception running application bikerentalsystem1.BikeRentalSystem1
E:\Project\BikeRentalSystem1\nbproject\build-impl.xml:1367: The following error occurred while executing this line:
E:\Project\BikeRentalSystem1\nbproject\build-impl.xml:962: Java returned: 1
BUILD FAILED (total time: 4 seconds)

My code

/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/javafx/FXML.java to edit this template
 */
package bikerentalsystem1;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.input.MouseEvent;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

/**
 *
 * @author HP
 */
public class BikeRentalSystem1 extends Application {
    private double x=0,y=0;
    @Override
    public void start(Stage stage) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
        
        Scene scene = new Scene(root);
        
        root.setOnMousePressed((MouseEvent event) -> {
        x=event.getSceneX();
        y=event.getSceneY();
        });
        
        root.setOnMouseDragged((MouseEvent event)-> {
        stage.setX(event.getScreenX()-x);
        stage.setY(event.getScreenY()-y);
        
        stage.setOpacity(.8);
        });
        
        root.setOnMouseReleased((MouseEvent event)-> {
        stage.setOpacity(1);
        });
        
        stage.initStyle(StageStyle.TRANSPARENT);
        
        stage.setScene(scene);
        stage.show();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }
    
}

And FXML file

<?xml version="1.0" encoding="UTF-8"?>

<?import de.jensd.fx.glyphs.fontawesome.FontAwesomeIconView?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.PasswordField?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.effect.ColorInput?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.StackPane?>
<?import javafx.scene.shape.Box?>
<?import javafx.scene.text.Font?>

<StackPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="450.0" prefWidth="700.0" xmlns="http://javafx.com/javafx/21" xmlns:fx="http://javafx.com/fxml/1">
   <children>
      <AnchorPane prefHeight="425.0" prefWidth="523.0">
         <children>
            <Box depth="100" height="450.0" layoutX="200.0" layoutY="225.0" width="400.0">
               <effect>
                  <ColorInput paint="WHITE" />
               </effect>
            </Box>
            <AnchorPane layoutX="350.0" prefHeight="450.0" prefWidth="350.0" styleClass="signinAnchor" stylesheets="@logicDesign.css">
               <children>
                  <FontAwesomeIconView glyphName="USER" layoutX="169.0" layoutY="104.0" size="5em" />
                  <Label layoutX="149.0" layoutY="126.0" text="Sign In">
                     <font>
                        <Font name="Berlin Sans FB" size="30.0" />
                     </font>
                  </Label>
                  <TextField layoutX="81.0" layoutY="180.0" prefHeight="30.0" prefWidth="220.0" promptText="Username" styleClass="textfield" stylesheets="@logicDesign.css" />
                  <PasswordField layoutX="81.0" layoutY="230.0" prefHeight="30.0" prefWidth="220.0" promptText="Password" styleClass="textfield" stylesheets="@logicDesign.css" />
                  <Button layoutX="140.0" layoutY="290.0" mnemonicParsing="false" prefHeight="30.0" prefWidth="100.0" styleClass="sign-in" stylesheets="@logicDesign.css" text="login" />
                  <Button layoutX="292.0" mnemonicParsing="false" prefHeight="29.0" prefWidth="58.0" styleClass="close" stylesheets="@logicDesign.css">
                     <graphic>
                        <FontAwesomeIconView fill="WHITE" glyphName="CLOSE" size="2em" />
                     </graphic>
                  </Button>
               </children></AnchorPane>
            <ImageView fitHeight="265.0" fitWidth="380.0" layoutY="185.0" pickOnBounds="true" preserveRatio="true">
               <image>
                  <Image url="@bajaj-pulsar-rs-220.jpg" />
               </image>
            </ImageView>
            <Box cullFace="FRONT" depth="100" height="50.0" layoutX="204.0" layoutY="95.0" width="360.0" />
            <Label alignment="CENTER" layoutX="14.0" layoutY="55.0" prefHeight="80.0" prefWidth="380.0" text="Bike Rental System" textFill="WHITE">
               <font>
                  <Font name="Century Schoolbook Bold Italic" size="35.0" />
               </font>
            </Label>
         </children>
      </AnchorPane>
   </children>
</StackPane>

please help i am new in JavaFX
i tried many things watched videos, read forms but couldn't find whats the problem

1

There are 1 best solutions below

0
jewelsea On

As noted in comments by James_D, Slaw and VGR.

The FontAwesome library version you are using is obsolete

The underlying problem is that the third-party FontAwesome library you are using relies on the class com.sun.javafx.css.parser.CSSParser.

That class was never part of the public API (so the library shouldn't really have been using it) and was removed entirely ages ago (I think in JavaFX version 9).

You need a more up to date version of that library, or (since it doesn't look like it is being currently maintained) find an alternative.

Alternate library

A viable alternative is ikonli.

ikonli:

  • works well with modern versions of JavaFX (as of JavaFX 21.0.1)
  • has documentation on integration with JavaFX
  • supports a wide variety of common, high-quality icon packs
  • contains well-defined modularity
    • allows you to define dependencies on just the icon packs you need.
    • works well with a Java Platform Module System (JPMS) module project (a type that is common for JavaFX applications).

Alternate approach using Unicode characters

Many icons in FontAwesome are now available as regular Unicode characters. You may not need any third-party libraries at all.

  • Though when running in Windows, it may be necessary to hard-code the Segoe UI or Segoe UI Symbol font.