3

I'm building an application using JavaFx and scene builder, however everything works fine, except when I add the Controller class.

I get the following error:

Exception in Application start method Exception in thread "main" java.lang.RuntimeException: Exception in Application start method at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:875) at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$147(LauncherImpl.java:157) at com.sun.javafx.application.LauncherImpl$$Lambda$1/868693306.run(Unknown Source) at java.lang.Thread.run(Thread.java:745) Caused by: javafx.fxml.LoadException: /C:/Users/M%20ROSE/Documents/Info%20Trivia/out/production/Info%20Trivia/sample/gameScene1.fxml:15

However the moment I remove the fx:controller attribute from this line in my fxml code it works perfectly.

<BorderPane maxHeight="450.0" maxWidth="800.0" minHeight="450.0" minWidth="800.0" prefHeight="400.0" prefWidth="800.0" styleClass="questionInstance" stylesheets="@style.css" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">

Here are the relevant codes

Controller Class

package sample;

import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.shape.* ;
import javafx.geometry.* ;
import javafx.scene.text.* ;
import javafx.scene.image.* ;
import javafx.scene.control.* ;
import java.lang.* ;
import javafx.scene.layout.* ;
import javafx.geometry.Insets ;
import javafx.scene.layout.GridPane ;
import javafx.scene.control.Button ;
import javafx.scene.control.Label ;

public class Controller {

    //Initialize fxml controls
    public Button trueButton;
    public Button falseButton;
    public Label playerLabel;
    public Label questionLabel;
    public Label scoreValue;
    public ImageView questionImage;
    public Rectangle redBar;
    public Rectangle greenBar;


    //Create array for level 1 questions

    String[][] levelOneData = {
        {"This is a Sequence Diagram","f", null},
        {"This diagram is for a database","t", null},
        {"This is a rack diagram","t", null},
        {"This is a flow chart","f", null},
        {"This is a kind of UML diagram","t", null}
    };
    Image[] levelOneImages = new Image[]  {
            new Image("res/images/l1q1.png"),
            new Image("res/images/l1q2.png"),
            new Image("res/images/l1q3.png"),
            new Image("res/images/l1q4.png"),
            new Image("res/images/l1q5.png")
    };


    public void levelOneInitializer(){
        questionLabel.setText(levelOneData[0][0]);
        questionImage.setImage(levelOneImages[0]);
        System.out.println("done");

    }
}

Main Class

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("gameScene1.fxml"));
        primaryStage.setTitle("Info Trivia");
        primaryStage.setScene(new Scene(root, 800, 450));
        primaryStage.setResizable(false);
        primaryStage.show();
    }


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

gameScene1.fxml

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

<?import javafx.scene.shape.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.text.*?>
<?import javafx.scene.image.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>

<BorderPane maxHeight="450.0" maxWidth="800.0" minHeight="450.0" minWidth="800.0" prefHeight="400.0" prefWidth="800.0" styleClass="questionInstance" stylesheets="@style.css" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
   <top>
      <HBox styleClass="questheader" BorderPane.alignment="CENTER">
         <children>
            <ImageView pickOnBounds="true" preserveRatio="true">
               <image>
                  <Image url="@res/images/userICon.png" />
               </image>
            </ImageView>
            <Label id="playerName" fx:id="playerLabel" text="Player 1" textFill="#a2c2b1">
               <font>
                  <Font name="Arial" size="24.0" />
               </font>
            </Label>
            <Region prefHeight="0.0" prefWidth="382.0" />
            <Label id="ScoreLabel" layoutX="40.0" layoutY="10.0" text="Score: " textFill="#a2c2b1">
               <font>
                  <Font name="Arial" size="24.0" />
               </font>
            </Label>
            <Label id="scoreValue" fx:id="scoreValue" layoutX="129.0" layoutY="10.0" text="0" textFill="#a2c2b1">
               <font>
                  <Font name="Arial" size="24.0" />
               </font>
            </Label>
         </children>
         <BorderPane.margin>
            <Insets left="50.0" right="50.0" />
         </BorderPane.margin>
         <padding>
            <Insets bottom="10.0" left="15.0" right="15.0" top="10.0" />
         </padding>
      </HBox>
   </top>
   <left>
      <StackPane prefHeight="150.0" prefWidth="200.0" BorderPane.alignment="CENTER">
         <BorderPane.margin>
            <Insets left="50.0" />
         </BorderPane.margin>
         <children>
            <ImageView fx:id="questionImage" pickOnBounds="true" preserveRatio="true">
               <image>
                  <Image url="@res/images/blankimage.png" />
               </image>
               <StackPane.margin>
                  <Insets left="35.0" />
               </StackPane.margin>
            </ImageView>
         </children>
      </StackPane>
   </left>
   <bottom>
      <HBox BorderPane.alignment="CENTER">
         <children>
            <Rectangle fx:id="greenBar" arcHeight="5.0" arcWidth="5.0" fill="#34b316" height="28.0" stroke="BLACK" strokeType="INSIDE" strokeWidth="0.0" width="10.0" />
            <Rectangle fx:id="redBar" arcHeight="5.0" arcWidth="5.0" fill="#a93535" height="28.0" stroke="BLACK" strokeType="INSIDE" strokeWidth="0.0" width="620.0" />

         </children>
         <padding>
            <Insets bottom="40.0" left="85.0" right="85.0" />
         </padding>
      </HBox>
   </bottom>
   <center>
      <BorderPane prefHeight="200.0" prefWidth="200.0" BorderPane.alignment="CENTER">
         <BorderPane.margin>
            <Insets right="85.0" />
         </BorderPane.margin>
         <top>
            <StackPane prefHeight="150.0" prefWidth="200.0" BorderPane.alignment="CENTER">
               <children>
                  <Label fx:id="questionLabel" text="This Is a Question" textFill="#191919">
                     <font>
                        <Font name="Arial" size="24.0" />
                     </font>
                  </Label>
               </children>
            </StackPane>
         </top>
         <center>
            <HBox prefHeight="100.0" prefWidth="200.0" BorderPane.alignment="CENTER">
               <children>
                  <Button fx:id="trueButton" mnemonicParsing="false" prefWidth="100.0" text="True">
                     <HBox.margin>
                        <Insets />
                     </HBox.margin>
                  </Button>
                  <Region prefWidth="200.0" />
                  <Button fx:id="falseButton" mnemonicParsing="false" prefWidth="100.0" text="False" />
               </children>
               <BorderPane.margin>
                  <Insets />
               </BorderPane.margin>
               <padding>
                  <Insets left="20.0" top="20.0" />
               </padding>
            </HBox>
         </center>
      </BorderPane>
   </center>
</BorderPane>
4
  • I think that you need to double check the name of the controller class specified in your fxml and make sure that it matches the actual name of the class. It looks like you're specifying sample.Controller and your class name is Controller. They need to match. Also, the '.' in the name is not a good idea. :) Commented Jun 8, 2015 at 4:35
  • Thanks @RonSiven. I finally got it to work, however it wasn't because of the controller name. It appears the arrays i had created in the controller class were causing it. Once i commented them out, it worked. However i'm lost on how i'm going to be able to create these arrays now. Commented Jun 8, 2015 at 4:53
  • if you solved the problem please add the answer and accept it (otherwise this will be marked as unanswered) Commented Jun 8, 2015 at 6:45
  • @AdeoluFola-Alade maybe the paths to your images are not correct? Do you have not stacktrace? Commented Jun 8, 2015 at 9:54

1 Answer 1

0

You controller have to implement Initializable, Try with the following :

public class Controller implement Initializable{

    //Initialize fxml controls
    @FXML
    public Button trueButton;
    @FXML
    public Button falseButton;
    @FXML
    public Label playerLabel;
    @FXML
    public Label questionLabel;
    @FXML
    public Label scoreValue;
    @FXML
    public ImageView questionImage;
    @FXML
    public Rectangle redBar;
    @FXML
    public Rectangle greenBar;


    //Create array for level 1 questions

    String[][] levelOneData = {
        {"This is a Sequence Diagram","f", null},
        {"This diagram is for a database","t", null},
        {"This is a rack diagram","t", null},
        {"This is a flow chart","f", null},
        {"This is a kind of UML diagram","t", null}
    };
    Image[] levelOneImages = new Image[]  {
            new Image("res/images/l1q1.png"),
            new Image("res/images/l1q2.png"),
            new Image("res/images/l1q3.png"),
            new Image("res/images/l1q4.png"),
            new Image("res/images/l1q5.png")
    };


    public void levelOneInitializer(){
        questionLabel.setText(levelOneData[0][0]);
        questionImage.setImage(levelOneImages[0]);
        System.out.println("done");

    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.