0

I'm trying to do an application with JavaFX. And I have a problem with NullPointerException. I tried every solutions, write the full path, move the FXMl file ... but i didn't find the solution.

Thanks in advance !

Here is the Main.java :

package eu.hautil.pigeonnier;

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 Main extends Application {
    @Override
    public void start(Stage primaryStage) throws IOException {
        Parent root = FXMLLoader.load(getClass().getClassLoader().getResource("src/main/resources/sample.fxml"));
        primaryStage.setTitle("Connexion");
        primaryStage.setScene(new Scene(root, 600, 500));

    }

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

Here is the sample.fxml :

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

<?import javafx.scene.text.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.image.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>

<HBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" style="-fx-background-color: #032445;" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="eu.hautil.pigeonnier.Controller">
   <children>
      <AnchorPane prefHeight="391.0" prefWidth="263.0" style="-fx-background-color: #000;">
         <children>
            <ImageView fitHeight="200.0" fitWidth="200.0" layoutX="32.0" layoutY="43.0" pickOnBounds="true" preserveRatio="true">
               <image>
                  <Image url="@../../../img/pigeon.png" />
               </image>
            </ImageView>
            <Label layoutX="10.0" layoutY="243.0" prefHeight="82.0" prefWidth="245.0" text="Connexion à McPigeon !" textFill="WHITE">
               <font>
                  <Font name="Arial Bold" size="21.0" />
               </font>
            </Label>
            <Label layoutX="7.0" layoutY="332.0" prefHeight="42.0" prefWidth="251.0" text="Edit by : Antoine Gervais, Mattéo Errin, Corentin Da Silva" textFill="WHITE">
               <font>
                  <Font name="Arial Bold" size="9.0" />
               </font>
            </Label>
         </children>
      </AnchorPane>
      <AnchorPane prefHeight="200.0" prefWidth="200.0">
         <children>
            <ImageView fitHeight="150.0" fitWidth="172.0" layoutX="86.0" pickOnBounds="true" preserveRatio="true">
               <image>
                  <Image url="@../../../img/lock.png" />
               </image>
            </ImageView>
            <Label alignment="CENTER" contentDisplay="CENTER" layoutX="48.0" layoutY="247.0" prefHeight="55.0" prefWidth="200.0" text="Mot de passe :" textAlignment="CENTER" textFill="WHITE" underline="true">
               <font>
                  <Font name="Arial Bold" size="23.0" />
               </font>
            </Label>
            <Label alignment="CENTER" contentDisplay="CENTER" layoutX="48.0" layoutY="158.0" prefHeight="42.0" prefWidth="200.0" text=" Utilisateur :" textAlignment="CENTER" textFill="WHITE" underline="true">
               <font>
                  <Font name="Arial Bold" size="23.0" />
               </font>
            </Label>
            <TextField fx:id="tf_username" layoutX="46.0" layoutY="200.0" style="-fx-background-color: DCDCDC;">
               <font>
                  <Font size="16.0" />
               </font>
            </TextField>
            <TextField fx:id="tf_password" layoutX="46.0" layoutY="295.0" style="-fx-background-color: #DCDCDC;">
               <font>
                  <Font size="16.0" />
               </font>
            </TextField>
            <Button fx:id="button_login" layoutX="55.0" layoutY="355.0" mnemonicParsing="false" prefHeight="39.0" prefWidth="186.0" text="Se connecter" />
         </children></AnchorPane>
   </children>
</HBox>

And finally this is the StackTrace :

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:78)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:567)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:464)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:363)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:78)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:567)
    at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1071)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:900)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:195)
    at java.base/java.lang.Thread.run(Thread.java:831)
Caused by: java.lang.NullPointerException: Location is required.
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3316)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3280)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3249)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3222)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3199)
    at javafx.fxml/javafx.fxml.FXMLLoader.load(FXMLLoader.java:3192)
    at eu.hautil.pigeonnier/eu.hautil.pigeonnier.Main.start(Main.java:14)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:846)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:474)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:447)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:446)
    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:174)
    ... 1 more
Exception running application eu.hautil.pigeonnier.Main

And a little picture of my Project folder :

Project Folder

GeRvaxx-
  • 3
  • 2
  • This is returning null: `getClass().getClassLoader().getResource("src/main/resources/sample.fxml")` Try searching for ___getresource javafx site:stackoverflow.com___ – Abra Dec 28 '21 at 15:14

0 Answers0