'problem I can't find a solution to it on javaFX [closed]

I am trying to learn javaFx for an academic reasons so I tried this little project but all I get is his error!

    Exception in Application start method
java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
    at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$159(LauncherImpl.java:182)
    at java.lang.Thread.run(Thread.java:748)
Caused by: javafx.fxml.LoadException: Root value already specified.
/C:/Users/Malik/Documents/NetBeansProjects/WorkshopPIDEV2122/build/classes/tests/AjoutPersonne.fxml

    at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2597)
    at javafx.fxml.FXMLLoader.createElement(FXMLLoader.java:2755)
    at javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2704)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2527)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2409)
    at tests.MainProgGUI.start(MainProgGUI.java:28)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$166(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$179(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$177(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$178(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$152(WinApplication.java:177)
    ... 1 more
Exception running application tests.MainProgGUI
C:\Users\Malik\AppData\Local\NetBeans\Cache\12.6\executor-snippets\run.xml:111: The following error occurred while executing this line:
C:\Users\Malik\AppData\Local\NetBeans\Cache\12.6\executor-snippets\run.xml:94: Java returned: 1
BUILD FAILED (total time: 0 seconds)

here is my code: FXML file:

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

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>

<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1">
   <children>
      <TextField fx:id="tfnom" layoutX="207.0" layoutY="103.0" />
      <TextField fx:id="tfprenom" layoutX="207.0" layoutY="176.0" />
      <Label layoutX="121.0" layoutY="108.0" text="Nom" />
      <Label layoutX="121.0" layoutY="181.0" text="Prenom" />
      <Button layoutX="259.0" layoutY="276.0" mnemonicParsing="false" onAction="AjoutPersonne" text="Button" />
   </children>
</AnchorPane>


 

FXML Controller:

    package tests;

import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;

/**
 * FXML Controller class
 *
 * @author Malik
 */
public class AjoutPersonneController implements Initializable {

    /**
     * Initializes the controller class.
     */
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        // TODO
    }    
     @FXML
    private TextField tfnom;

    @FXML
    private TextField tfprenom;

    @FXML
    void AjoutPersonne(ActionEvent event) {

    }

}

and my mainprog :

    /*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/javafx/FXMain.java to edit this template
 */
package tests;

import java.io.IOException;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

/**
 *
 * @author Malik
 */
public class MainProgGUI extends Application {
    
    public void start(Stage primaryStage) throws IOException {
    FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("AjoutPersonne.fxml"));
    fxmlLoader.setRoot(new AnchorPane());       
    Parent root = fxmlLoader.load();
    Scene scene = new Scene(root);

  
    primaryStage.setTitle("Workshop PIDEV");
    primaryStage.setScene(scene);
    primaryStage.show();
    
}

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

this is the architecture of programme: enter image description here

Ps: I tried to change the location of FXML file and to use the Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml")); thanks for helping !



Solution 1:[1]

You have a few errors:

After making those changes, the app worked for me:

module-info.java

module test {
    requires javafx.controls;
    requires javafx.fxml;

    opens test to javafx.fxml;
    exports test;
}

MainProgGUI.java

package test;

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

import java.io.IOException;

public class MainProgGUI extends Application {

    public void start(Stage stage) throws IOException {
        FXMLLoader loader = new FXMLLoader(
                getClass().getResource(
                        "AjoutPersonne.fxml"
                )
        );

        Parent root = loader.load();

        stage.setScene(new Scene(root));
        stage.show();
    }

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

}

AjoutPersonneController.java

package test;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.TextField;

public class AjoutPersonneController {
    @FXML
    private TextField tfnom;

    @FXML
    private TextField tfprenom;

    @FXML
    private void ajoutPersonne(ActionEvent event) {
        System.out.println(event);
    }
}

AjoutPersonne.fxml

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

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>

<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0"
            xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1"
            fx:controller="test.AjoutPersonneController">
    <children>
        <TextField fx:id="tfnom" layoutX="207.0" layoutY="103.0" />
        <TextField fx:id="tfprenom" layoutX="207.0" layoutY="176.0" />
        <Label layoutX="121.0" layoutY="108.0" text="Nom" />
        <Label layoutX="121.0" layoutY="181.0" text="Prenom" />
        <Button layoutX="259.0" layoutY="276.0" mnemonicParsing="false" onAction="#ajoutPersonne" text="Button" />
    </children>
</AnchorPane>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1