javaexceptionjavafxfxmlinvocationtargetexception

Why cant I start a JavaFX program?


I have a little problem with my program. I can run the program normally until i give my elements tags and actions like "onClick"... There is some kind of exception that i can't fix so I'm asking for your help. Is the problem just in my computer or it's the code fault ?

Here's all you need :

Exception :

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: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: 
/E:/programovanie/netgame/out/production/netgame/sample/sample.fxml:26

    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: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 sample.Main.start(Main.java:13)
    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
Caused by: java.lang.IllegalArgumentException: Can not set org.w3c.dom.Text field sample.Controller.minuty to javafx.scene.text.Text
    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)
    ... 17 more
Exception running application sample.Main

sample.fxml :

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

<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Slider?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.text.Font?>
<?import javafx.scene.text.Text?>

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="600.0" prefWidth="900.0" xmlns="http://javafx.com/javafx/8.0.172-ea" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
    <children>
        <GridPane minHeight="100.0" minWidth="900.0" style="-fx-background-color: #CDE8C8; -fx-border-color: black;">
            <columnConstraints>
                <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
            </columnConstraints>
            <rowConstraints>
                <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
            </rowConstraints>
            <children>
                <Text fx:id="minuty" strokeType="OUTSIDE" strokeWidth="0.0" text="00" wrappingWidth="225.125">
                    <font>
                        <Font size="40.0" />
                    </font>
                    <GridPane.margin>
                        <Insets left="50.0" />
                    </GridPane.margin>
                </Text>
                <Text strokeType="OUTSIDE" strokeWidth="0.0" text=":" textAlignment="CENTER" wrappingWidth="224.671875">
                    <font>
                        <Font size="40.0" />
                    </font>
                </Text>
                <Text fx:id="sekundy" strokeType="OUTSIDE" strokeWidth="0.0" text="00" textAlignment="RIGHT" wrappingWidth="176.9375">
                    <font>
                        <Font size="40.0" />
                    </font>
                    <GridPane.margin>
                        <Insets />
                    </GridPane.margin>
                </Text>
                <Text fx:id="stavcerveny" fill="RED" strokeType="OUTSIDE" strokeWidth="0.0" text="0" wrappingWidth="225.5625" GridPane.columnIndex="1">
                    <font>
                        <Font size="40.0" />
                    </font>
                    <GridPane.margin>
                        <Insets left="50.0" />
                    </GridPane.margin>
                </Text>
                <Text strokeType="OUTSIDE" strokeWidth="0.0" text=":" textAlignment="CENTER" wrappingWidth="225.671875" GridPane.columnIndex="1">
                    <font>
                        <Font size="40.0" />
                    </font>
                </Text>
                <Text fx:id="stavmodry" fill="#0073ff" strokeType="OUTSIDE" strokeWidth="0.0" text="0" textAlignment="RIGHT" wrappingWidth="157.5625" GridPane.columnIndex="1">
                    <font>
                        <Font size="40.0" />
                    </font>
                </Text>
                <Button alignment="CENTER" mnemonicParsing="false" onMouseClicked="#zapnutie" prefHeight="25.0" prefWidth="226.0" text="PLAY" GridPane.columnIndex="2">
                    <GridPane.margin>
                        <Insets bottom="40.0" left="40.0" right="40.0" />
                    </GridPane.margin>
                </Button>
                <Button mnemonicParsing="false" onMouseClicked="#restart" prefHeight="25.0" prefWidth="227.0" text="RESTART" textAlignment="CENTER" GridPane.columnIndex="2">
                    <opaqueInsets>
                        <Insets />
                    </opaqueInsets>
                    <GridPane.margin>
                        <Insets left="40.0" right="40.0" top="50.0" />
                    </GridPane.margin>
                </Button>
                <Slider fx:id="obtiazne" prefHeight="14.0" prefWidth="126.0" GridPane.columnIndex="3">
                    <GridPane.margin>
                        <Insets left="20.0" right="20.0" />
                    </GridPane.margin>
                </Slider>
            </children>
        </GridPane>
      <AnchorPane fx:id="hra" layoutY="100.0" prefHeight="500.0" prefWidth="900.0" />
    </children>
</AnchorPane>

Controller.java :

package sample;

import javafx.fxml.FXML;
import javafx.scene.control.Slider;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import org.w3c.dom.Text;

public class Controller {
    @FXML
    Slider obtiazne;
    @FXML
    Text minuty;
    @FXML
    Text sekundy;
    @FXML
    Text stavcerveny;
    @FXML
    Text stavmodry;
    @FXML
    AnchorPane hra;

    public void zapnutie(MouseEvent mouseEvent) {
        System.out.println(obtiazne.getValue());
    }

    public void restart(MouseEvent mouseEvent) {
    }
}

Main.java :

package sample;

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 primaryStage) throws Exception{
        Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
        primaryStage.setTitle("Hello World");
        primaryStage.setScene(new Scene(root));
        primaryStage.show();
    }


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

I think the right rows are :

"at sample.Main.start(Main.java:13)"

and that's : " Parent root = FXMLLoader.load(getClass().getResource("sample.fxml")); "

I don't understand the rest of it so I hope, that you can easily get through the problem :)


Solution

  • The part of the stack trace that's relevant is

    Can not set org.w3c.dom.Text field sample.Controller.minuty to javafx.scene.text.Text

    Which means your imports are wrong in the controller class

    Change import org.w3c.dom.Text; to import javafx.scene.text.Text;