user16361709
user16361709

Reputation:

FxmlLoader.load() method returns InvocationTargetException

Here's my project on the Secrets app(I'm trying to build the login/signup page, but this blocks my way!). The error code is pasted after it. Thanks!

Main.java -
package com.example.secret;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.stage.Stage;

import java.io.IOException;

public class Main extends Application {
    @Override
    public void start(Stage stage) throws IOException {
        FXMLLoader fxmlLoader = new FXMLLoader(Main.class.getResource("main.fxml"));
        Scene scene = new Scene(fxmlLoader.load(), 320, 240);
        stage.setTitle("Hello!");
        stage.setScene(scene);
        stage.show();
    }

    public static void main(String[] args) {
        launch();
    }
}

Main.fxml -

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

<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.effect.Blend?>
<?import javafx.scene.effect.Glow?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>

<VBox alignment="CENTER" prefHeight="692.0" prefWidth="868.0" spacing="20.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/16" fx:controller="com.example.secret.MainController">
    <padding>
        <Insets bottom="20.0" left="20.0" right="20.0" top="20.0" />
    </padding>
   <children>
      <Label text="Secret Vault!" textAlignment="CENTER" textFill="#0066ff">
         <font>
            <Font name="MV Boli" size="48.0" />
         </font>
         <effect>
            <Blend mode="SOFT_LIGHT">
               <topInput>
                  <Glow level="0.62" />
               </topInput>
            </Blend>
         </effect>
      </Label>
       <HBox prefHeight="673.0" prefWidth="828.0">
           <AnchorPane prefHeight="558.0" prefWidth="225.0">
               <ImageView fitHeight="89.0" fitWidth="150.0" layoutX="101.0" layoutY="14.0" pickOnBounds="true"
                          preserveRatio="true">
                   <image>
                       <Image url="@Image/secure-blue-icon.png"/>
                   </image>
               </ImageView>
               <AnchorPane layoutY="122.0" prefHeight="436.0" prefWidth="200.0">
                   <Label layoutX="8.0" prefHeight="60.0" prefWidth="282.0" text="Your secrets, in your vault,"
                          textFill="#0066ff">
                       <font>
                           <Font name="Ink Free" size="24.0"/>
                       </font>
                       <effect>
                           <Blend mode="SOFT_LIGHT">
                               <topInput>
                                   <Glow level="0.62"/>
                               </topInput>
                           </Blend>
                       </effect>
                   </Label>
                   <Label layoutX="63.0" layoutY="38.0" prefHeight="60.0" prefWidth="164.0"
                          text="saved securely!" textFill="#0066ff">
                       <font>
                           <Font name="Ink Free" size="24.0"/>
                       </font>
                       <effect>
                           <Blend mode="SOFT_LIGHT">
                               <topInput>
                                   <Glow level="0.62"/>
                               </topInput>
                           </Blend>
                       </effect>
                   </Label>
                   <ImageView fitHeight="89.0" fitWidth="153.0" layoutX="100.0" layoutY="98.0"
                              pickOnBounds="true" preserveRatio="true">
                       <image>
                           <Image url="@Image/blue-key-icon.png"/>
                       </image>
                   </ImageView>
                   <Label layoutX="57.0" layoutY="196.0" prefHeight="60.0" prefWidth="184.0"
                          text="Who has the key?" textFill="#0066ff">
                       <font>
                           <Font name="Ink Free" size="24.0"/>
                       </font>
                       <effect>
                           <Blend mode="SOFT_LIGHT">
                               <topInput>
                                   <Glow level="0.62"/>
                               </topInput>
                           </Blend>
                       </effect>
                   </Label>
                   <Label layoutX="75.0" layoutY="233.0" prefHeight="60.0" prefWidth="149.0"
                          text="YOU, yes, only!" textFill="#0066ff">
                       <font>
                           <Font name="Ink Free" size="24.0"/>
                       </font>
                       <effect>
                           <Blend mode="SOFT_LIGHT">
                               <topInput>
                                   <Glow level="0.62"/>
                               </topInput>
                           </Blend>
                       </effect>
                   </Label>
                   <Button layoutX="86.0" layoutY="315.0" mnemonicParsing="false" text="Animate" fx:id="button-animate">
                       <font>
                           <Font name="Cambria" size="24.0"/>
                       </font>
                   </Button>
               </AnchorPane>
           </AnchorPane>
       </HBox>
   </children>
</VBox>

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:78)
    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:78)
    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:1071)
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:831)
Caused by: javafx.fxml.LoadException: Invalid identifier.
/C:/Users/{myname}/Desktop/Netbrain%20IDE%20Projects/Secret/target/classes/com/example/secret/main.fxml:99

    at javafx.fxml/javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2703)
    at javafx.fxml/javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:911)
    at javafx.fxml/javafx.fxml.FXMLLoader$InstanceDeclarationElement.processAttribute(FXMLLoader.java:982)
    at javafx.fxml/javafx.fxml.FXMLLoader$Element.processStartElement(FXMLLoader.java:229)
    at javafx.fxml/javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:754)
    at javafx.fxml/javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2808)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2634)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548)
    at javafx.fxml/javafx.fxml.FXMLLoader.load(FXMLLoader.java:2517)
    at com.example.secret/com.example.secret.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:474)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:447)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:446)
    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 com.example.secret.Main

Thanks for you help! I know what it's like to be stuck on a error, but maybe I shouls start helping people because I mostly ask, ask ask!

Upvotes: 1

Views: 94

Answers (1)

Abra
Abra

Reputation: 20924

The stack trace tells you that there is a problem in line 99 of file main.fxml.

/C:/Users/{myname}/Desktop/Netbrain%20IDE%20Projects/Secret/target/classes/com/example/secret/main.fxml:99

I believe that means the following line:

<Button layoutX="86.0" layoutY="315.0" mnemonicParsing="false" text="Animate" fx:id="button-animate">

The value for attribute fx:id is button-animate. The hyphen (or dash) character, i.e. -, is not a valid character for java identifiers. This is also mentioned in the following line from the stack trace.

Caused by: javafx.fxml.LoadException: Invalid identifier

Maybe you should consider using java naming conventions.

In any case, the stack trace is your friend. You need to learn how to read and interpret it.

Upvotes: 3

Related Questions