javajavafxinvocationtargetexception

InvocationTargetException Error with Basic Java(FX) Project


I am trying to make a simple dice throwing game using Java and JavaFX.
I have created the scenes, and made a Main java class and a Java controller, however no matter how much I try I keep getting the same errors - which I have tried resolving in many ways so just trying to see if anybody could lend me some guidance in the right direction! Below are my classes and FXML code!

main.java

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 {

    public void start(Stage primaryStage) throws IOException{
        FXMLLoader loader = new FXMLLoader(getClass().getResource("welcome.fxml"));
        Parent root = (Parent) loader.load();
        primaryStage.setScene(new Scene(root));
        primaryStage.show();

    }

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

welcomecontroller.java

import java.io.IOException;   
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.image.ImageView;
import javafx.stage.Stage;
import javafx.scene.control.TextField;

public class welcomecontroller {

    @FXML
    private ImageView p1_icon;

    @FXML
    private ImageView p2_icon;

    @FXML
    private ImageView end_icon;

    @FXML
    private ImageView die_animation;

    @FXML
    private Button btn_help;

    @FXML
    private Button btn_exit;

    @FXML
    private Button btn_start;

    @FXML
    void leftClick(ActionEvent event) {
        FXMLLoader loader = new FXMLLoader(getClass().getResource("main.fxml"));
        try {
            Parent root=(Parent) loader.load();
            Stage stage = new Stage();
            stage.setScene(new Scene(root));
            stage.show();
        }
        catch(IOException e)
        {
            e.printStackTrace();
        }
}

    @FXML
    private TextField field_player1;

//field_player1.getText(p2.setName());
    @FXML
    private TextField field_player2;

    @FXML
    private TextField field_endscore;        

}

welcome.fxml

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

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?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?>

<GridPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="welcomecontroller.java">
  <columnConstraints>
    <ColumnConstraints hgrow="SOMETIMES" maxWidth="272.0" minWidth="0.0" prefWidth="57.0" />
    <ColumnConstraints hgrow="SOMETIMES" maxWidth="486.0" minWidth="10.0" prefWidth="278.0" />
      <ColumnConstraints hgrow="SOMETIMES" maxWidth="305.0" minWidth="0.0" prefWidth="87.0" />
      <ColumnConstraints hgrow="SOMETIMES" maxWidth="305.0" minWidth="10.0" prefWidth="179.0" />
      <ColumnConstraints hgrow="SOMETIMES" maxWidth="221.0" minWidth="0.0" prefWidth="43.0" />
      <ColumnConstraints hgrow="SOMETIMES" maxWidth="221.0" minWidth="10.0" prefWidth="26.0" />
  </columnConstraints>
  <rowConstraints>
      <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
      <RowConstraints maxHeight="50.0" minHeight="10.0" prefHeight="47.0" vgrow="SOMETIMES" />
    <RowConstraints maxHeight="53.0" minHeight="10.0" prefHeight="53.0" vgrow="SOMETIMES" />
    <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
    <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
      <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
      <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
      <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
      <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
  </rowConstraints>
   <children>
      <Text strokeType="OUTSIDE" strokeWidth="0.0" text="DICE GAME" textAlignment="CENTER" translateX="75.0" translateY="5.0" GridPane.columnIndex="1">
         <font>
            <Font name="Arial Black" size="29.0" />
         </font>
      </Text>
      <Text strokeType="OUTSIDE" strokeWidth="0.0" GridPane.columnIndex="1" GridPane.rowIndex="1" />
      <Text opacity="0.37" strokeType="OUTSIDE" strokeWidth="0.0" text="Made by emm35" translateX="265.0" translateY="6.0" wrappingWidth="92.23500061035156" GridPane.columnIndex="1" />
      <Text strokeType="OUTSIDE" strokeWidth="0.0" text="Player 1 Name:" translateX="65.0" GridPane.columnIndex="1" GridPane.rowIndex="2">
         <font>
            <Font name="System Bold Italic" size="17.0" />
         </font>
      </Text>
      <Text strokeType="OUTSIDE" strokeWidth="0.0" text="Player 2 Name:" translateX="65.0" GridPane.columnIndex="1" GridPane.rowIndex="4">
         <font>
            <Font name="System Bold" size="17.0" />
         </font>
      </Text>
      <Text strokeType="OUTSIDE" strokeWidth="0.0" text="End Game score:" translateX="65.0" GridPane.columnIndex="1" GridPane.rowIndex="6">
         <font>
            <Font name="System Bold" size="17.0" />
         </font>
      </Text>
      <Button fx:id="btn_exit" mnemonicParsing="false" text="EXIT GAME" translateX="60.0" GridPane.columnIndex="3" GridPane.rowIndex="3" />
      <Button fx:id="btn_help" mnemonicParsing="false" text="HELP/TUTORIAL" translateX="45.0" GridPane.columnIndex="3" GridPane.rowIndex="5" />
      <Button fx:id="btn_start" mnemonicParsing="false" text="START GAME" translateX="55.0" GridPane.columnIndex="3" GridPane.rowIndex="7" />
      <ImageView fitHeight="40.0" fitWidth="40.0" translateX="5.0" GridPane.rowIndex="7">
         <image>
            <Image url="@end.png" />
         </image>
      </ImageView>
      <ImageView fitHeight="40.0" fitWidth="40.0" translateX="5.0" GridPane.rowIndex="5">
         <image>
            <Image url="@player2.png" />
         </image>
      </ImageView>
      <ImageView fitHeight="40.0" fitWidth="40.0" translateX="5.0" GridPane.rowIndex="3">
         <image>
            <Image url="@player1.png" />
         </image>
      </ImageView>
      <ImageView fitHeight="109.0" fitWidth="103.0" translateX="45.0" GridPane.columnIndex="3" GridPane.rowIndex="1">
         <image>
            <Image url="@welcome_die.gif" />
         </image>
      </ImageView>
      <TextField fx:id="field_player1" GridPane.columnIndex="1" GridPane.rowIndex="3" />
      <TextField fx:id="field_player2" GridPane.columnIndex="1" GridPane.rowIndex="5" />
      <TextField fx:id="field_endscore" GridPane.columnIndex="1" GridPane.rowIndex="7" />
   </children>
</GridPane>

They are all within the src/same directory as shown in the screenshot below! Any advice is much appreciated to resolve this as I have tried lots of things! Were using the Scene builder to create the scenes as from here.

Image of my structure

enter image description here

Thank you in advance!


Solution

  • The problem seems to be the controller path.

    Replace fx:controller="welcomecontroller.java" with fx:controller="emm35_dicegame.welcomecontroller"

    It worked for me.