0

I've tried all the advice on Stack Overflow, but it didn't help to solve this problem. Whenever I try to run the main.java file, it throws an exception.

The code is shown below. Has anyone faced this issue before? main.java>>


import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import ru.javabegin.training.fastjava2.javafx.controllers.MainController;
import ru.javabegin.training.fastjava2.javafx.objects.Lang;
import ru.javabegin.training.fastjava2.javafx.utils.LocaleManager;

import java.io.IOException;
import java.util.Locale;
import java.util.Observable;
import java.util.Observer;
import java.util.ResourceBundle;

public class Main extends Application implements Observer {


    private static final String FXML_MAIN = "../fxml/main.fxml";
    public static final String BUNDLES_FOLDER = "ru.javabegin.training.fastjava2.javafx.bundles.Locale";

    private Stage primaryStage;

    private Parent fxmlMain;

    private MainController mainController;
    private FXMLLoader fxmlLoader;

    private VBox currentRoot;

    @Override
    public void start(Stage primaryStage) throws Exception {
        this.primaryStage = primaryStage;
        createGUI(LocaleManager.RU_LOCALE);
    }


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

    @Override
    public void update(Observable o, Object arg) {
        Lang lang = (Lang) arg;
        VBox newNode = loadFXML(lang.getLocale()); // получить новое дерево компонетов с нужной локалью
        currentRoot.getChildren().setAll(newNode.getChildren());// заменить старые дочерник компонента на новые - с другой локалью
    }



    // загружает дерево компонентов и возвращает в виде VBox (корневой элемент в FXML)
    private VBox loadFXML(Locale locale) {
        fxmlLoader = new FXMLLoader();

        fxmlLoader.setLocation(getClass().getResource("fxml/main.fxml"));
        fxmlLoader.setResources(ResourceBundle.getBundle(BUNDLES_FOLDER, locale));

        VBox node = null;

        try {
            node = (VBox) fxmlLoader.load();

            mainController = fxmlLoader.getController();
            mainController.addObserver(this);
            primaryStage.setTitle(fxmlLoader.getResources().getString("address_book"));

        } catch (IOException e) {
            e.printStackTrace();
        }

        return node;
    }

    private void createGUI(Locale locale) {
        currentRoot = loadFXML(locale);
        Scene scene = new Scene(currentRoot, 300, 275);
        primaryStage.setScene(scene);
        primaryStage.setMinHeight(600);
        primaryStage.setMinWidth(400);
        primaryStage.show();
    }
}

main.fxml>>


<?import java.lang.*?>
<?import org.controlsfx.control.textfield.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import org.controlsfx.control.textfield.CustomTextField?>

<VBox alignment="CENTER" maxHeight="-Infinity" maxWidth="-Infinity" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="ru.javabegin.training.fastjava2.javafx.controllers.MainController">
    <children>
        <HBox maxHeight="50.0" prefHeight="100.0">
            <children>
                <Button fx:id="btnAdd" minWidth="80.0" mnemonicParsing="false" onAction="#actionButtonPressed" text="%add">
                    <HBox.margin>
                        <Insets bottom="20.0" left="20.0" right="20.0" top="20.0" />
                    </HBox.margin>
                </Button>
                <Button fx:id="btnEdit" minWidth="80.0" mnemonicParsing="false" onAction="#actionButtonPressed" text="%edit">
                    <HBox.margin>
                        <Insets bottom="20.0" left="20.0" right="20.0" top="20.0" />
                    </HBox.margin>
                </Button>
                <Button fx:id="btnDelete" minWidth="80.0" mnemonicParsing="false" onAction="#actionButtonPressed" text="%delete">
                    <HBox.margin>
                        <Insets bottom="20.0" left="20.0" right="20.0" top="20.0" />
                    </HBox.margin>
                </Button>
            </children>
        </HBox>
        <AnchorPane maxHeight="30.0" prefHeight="100.0">
            <children>
                <CustomTextField fx:id="txtSearch" prefHeight="31.0" prefWidth="293.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="80.0" />
                <Button fx:id="btnSearch" layoutX="266.0" mnemonicParsing="false" onAction="#actionSearch" text="%search" AnchorPane.rightAnchor="0.0" />
            </children>
            <VBox.margin>
                <Insets bottom="20.0" left="20.0" right="20.0" top="20.0" />
            </VBox.margin>
        </AnchorPane>
        <AnchorPane prefHeight="100.0" prefWidth="200.0" VBox.vgrow="ALWAYS">
            <children>
                <TableView fx:id="tableAddressBook" prefHeight="400.0" prefWidth="357.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
                    <columns>
                        <TableColumn fx:id="columnFIO" minWidth="100.0" text="%fio" />
                        <TableColumn fx:id="columnPhone" minWidth="100.0" prefWidth="-1.0" text="%phone" />
                    </columns>
                    <columnResizePolicy>
                        <TableView fx:constant="CONSTRAINED_RESIZE_POLICY" />
                    </columnResizePolicy>
                </TableView>
            </children>
            <VBox.margin>
                <Insets bottom="20.0" left="20.0" right="20.0" top="20.0" />
            </VBox.margin>
        </AnchorPane>
      <AnchorPane prefHeight="30.0" prefWidth="200.0">
         <children>
                <Label fx:id="labelCount" text="%count" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" />
            <ComboBox fx:id="comboLocales" layoutX="229.0" layoutY="-1.0" prefHeight="31.0" prefWidth="144.0" AnchorPane.bottomAnchor="0.0" AnchorPane.rightAnchor="0.0" />
         </children>
         <VBox.margin>
            <Insets bottom="20.0" left="20.0" right="20.0" top="20.0" />
         </VBox.margin>
      </AnchorPane>
    </children>
</VBox>

Error>>

Exception in Application start method
java.lang.reflect.InvocationTargetException
    at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:119)
    at java.base/java.lang.reflect.Method.invoke(Method.java:578)
    at javafx.graphics@20.0.1/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:464)
    at javafx.graphics@20.0.1/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:363)
    at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104)
    at java.base/java.lang.reflect.Method.invoke(Method.java:578)
    at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1081)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at javafx.graphics@20.0.1/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:893)
    at javafx.graphics@20.0.1/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:195)
    at java.base/java.lang.Thread.run(Thread.java:1589)
Caused by: java.lang.IllegalStateException: Location is not set.
    at javafx.fxml@20.0.1/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2556)
    at javafx.fxml@20.0.1/javafx.fxml.FXMLLoader.load(FXMLLoader.java:2531)
    at ru.javabegin.training.fastjava2.javafx.start.Main.loadFXML(Main.java:64)
    at ru.javabegin.training.fastjava2.javafx.start.Main.createGUI(Main.java:78)
    at ru.javabegin.training.fastjava2.javafx.start.Main.start(Main.java:37)
    at javafx.graphics@20.0.1/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:839)
    at javafx.graphics@20.0.1/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:483)
    at javafx.graphics@20.0.1/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:456)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
    at javafx.graphics@20.0.1/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:455)
    at javafx.graphics@20.0.1/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
Exception running application ru.javabegin.training.fastjava2.javafx.start.Main

Process finished with exit code 1

I've tried to change main.fxml file path many times but the result is same. versions: Java openJDK 19 LTS-17 controlsfx-8.40.12 javafx-sdk-20.0.1

vm options set to --module-path $JavaFX19$ --add-modules javafx.controls,javafx.fxml

Aziret
  • 1
  • 5
    Start with a working `FXML` situation and modify the code from there. https://openjfx.io/openjfx-docs/, or read the potential duplicate. – SedJ601 May 02 '23 at 22:58
  • 3
    *"I've tried all the advice on Stack Overflow"* **All** of it? That is a very bold statement. *"Has anyone faced this issue before?"* There is a question just three below yours (in the default order viewing the javafx tag) with the exact error message you are seeing in the question title. It is one of the most commonly asked issues for this tag. – James_D May 03 '23 at 14:03

0 Answers0