Não consigo resolver esse erro: javafx.fxml.LoadException:

Estou tentando inserir uma imagem num formulário logo após a mostra de uma Splash screen, entretanto, começou a dar um erro, no qual já tentei de tudo, mas não consigo resolver de jeito algum (já tentei procurar a solução em outros lugares).

Códigos:

Controller

package sample;

import javafx.application.Platform;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.stage.Stage;

import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;

public class Controller implements Initializable {

@FXML
private ImageView imView;


@FXML
public void initialize() {

    imView.setImage(new Image("/images/NeedForSpeed.jpg"));

}


@Override
public void initialize(URL url, ResourceBundle resourceBundle) {

    new SplashScreen().start();
}

class SplashScreen extends Thread {
@Override
public void run() {
try {
Thread.sleep(2000);

            Platform.runLater(new Runnable() {
                @Override
                public void run() {

                    Parent root = null;
                    try {
                        root = FXMLLoader.load(getClass().getResource("ui/dashboard.fxml"));
                    } catch (IOException e) {
                        e.printStackTrace();
                    }


                    Scene scene = new Scene(root);
                    Stage stage = new Stage();
                    stage.setScene(scene);
                    scene.getStylesheets().add(getClass().getResource("css/interface.css").toExternalForm());
                    stage.show();
                    imView.getScene().getWindow().hide();
                }
            });
        } catch (InterruptedException e) {

            e.printStackTrace();
        }
    }
}

}

Main

package sample;

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

public class Main extends Application {

@Override
public void start(Stage primaryStage) throws Exception{
    Parent root = FXMLLoader.load(getClass().getResource("/sample/sample.fxml"));
    primaryStage.initStyle(StageStyle.UNDECORATED);
    primaryStage.setTitle("Hello World");
    primaryStage.setScene(new Scene(root, 1200, 700));
    primaryStage.setResizable(false);
    primaryStage.show();




}


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

}

dashboard.fxml

<?xml version="1.0" encoding="UTF-8"?> <?import javafx.scene.image.Image?> <?import javafx.scene.image.ImageView?> <?import javafx.scene.layout.AnchorPane?>

dashboardController

package sample.ui;

import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;

import java.net.URL;
import java.util.ResourceBundle;

public class controllerDashboard implements Initializable {

@FXML
private ImageView burguer;

@FXML
public void initialize(){

    burguer.setImage(new Image("/images/cross-out.png"));
    burguer.getStyleClass().add("css/interface.css");
}

@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
    burguer.setOnMouseEntered((MouseEvent e) ->
    {
        burguer.setStyle("-fx-background-color:#cc2a49;");
    });
}

}

Erro Apresentado:

“C:\Program Files\Java\jdk-11.0.5\bin\java.exe” --add-modules javafx.base,javafx.graphics --add-reads javafx.base=ALL-UNNAMED --add-reads javafx.graphics=ALL-UNNAMED “-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2019.3\lib\idea_rt.jar=52362:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2019.3\bin” -Dfile.encoding=UTF-8 -p C:\Users\lucas\Documents\Libraries\openjfx-11.0.2_windows-x64_bin-sdk\javafx-sdk-11.0.2\lib\javafx.base.jar;C:\Users\lucas\Documents\Libraries\openjfx-11.0.2_windows-x64_bin-sdk\javafx-sdk-11.0.2\lib\javafx.graphics.jar;C:\Users\lucas\Downloads\openjfx-11.0.2_windows-x64_bin-sdk\javafx-sdk-11.0.2;C:\Users\lucas\IdeaProjects\LauncherNeed\out\production\LauncherNeed;C:\Users\lucas\Documents\Libraries\openjfx-11.0.2_windows-x64_bin-sdk\javafx-sdk-11.0.2\lib\javafx-swt.jar;C:\Users\lucas\Documents\Libraries\openjfx-11.0.2_windows-x64_bin-sdk\javafx-sdk-11.0.2\lib\javafx.controls.jar;C:\Users\lucas\Documents\Libraries\openjfx-11.0.2_windows-x64_bin-sdk\javafx-sdk-11.0.2\lib\javafx.fxml.jar;C:\Users\lucas\Documents\Libraries\openjfx-11.0.2_windows-x64_bin-sdk\javafx-sdk-11.0.2\lib\javafx.media.jar;C:\Users\lucas\Documents\Libraries\openjfx-11.0.2_windows-x64_bin-sdk\javafx-sdk-11.0.2\lib\javafx.swing.jar;C:\Users\lucas\Documents\Libraries\openjfx-11.0.2_windows-x64_bin-sdk\javafx-sdk-11.0.2\lib\javafx.web.jar -m LauncherNeed/sample.Main
javafx.fxml.LoadException:
/C:/Users/lucas/IdeaProjects/LauncherNeed/out/production/LauncherNeed/sample/ui/dashboard.fxml:8

at javafx.fxml/javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2625)
at javafx.fxml/javafx.fxml.FXMLLoader.access$700(FXMLLoader.java:105)
at javafx.fxml/javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:943)
at javafx.fxml/javafx.fxml.FXMLLoader$InstanceDeclarationElement.processAttribute(FXMLLoader.java:980)
at javafx.fxml/javafx.fxml.FXMLLoader$Element.processStartElement(FXMLLoader.java:227)
at javafx.fxml/javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:752)
at javafx.fxml/javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2722)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2552)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2466)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3237)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3194)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3163)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3136)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3113)
at javafx.fxml/javafx.fxml.FXMLLoader.load(FXMLLoader.java:3106)
at LauncherNeed/sample.Controller$SplashScreen$1.run(Controller.java:48)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
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)
at java.base/java.lang.Thread.run(Thread.java:834)

Caused by: java.lang.IllegalAccessException: class javafx.fxml.FXMLLoader$ValueElement (in module javafx.fxml) cannot access class sample.ui.controllerDashboard (in module LauncherNeed) because module LauncherNeed does not export sample.ui to module javafx.fxml
at java.base/jdk.internal.reflect.Reflection.newIllegalAccessException(Reflection.java:361)
at java.base/jdk.internal.reflect.Reflection.ensureMemberAccess(Reflection.java:99)
at java.base/java.lang.Class.newInstance(Class.java:579)
at javafx.fxml/javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:936)
… 20 more
Exception in thread “JavaFX Application Thread” java.lang.NullPointerException: Root cannot be null
at javafx.graphics/javafx.scene.Scene.(Scene.java:345)
at javafx.graphics/javafx.scene.Scene.(Scene.java:207)
at LauncherNeed/sample.Controller$SplashScreen$1.run(Controller.java:54)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
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)
at java.base/java.lang.Thread.run(Thread.java:834)

Process finished with exit code 0

Essa mensagem de erro indica que não está localizando o fxml

1.Verifica se esta mesmo no diretorio sample
Se não tiver e sua ide for o NetBeans usa com o botão direito do mouse sobre o projeto Clean and Build

2.Caso o fxml esteja no diretorio correto então
no lugar de /sample/sample.fxml tenta sample/sample.fxml, enfim …

Tambem dentro do Controller

// Isso tem a ver com o caminho das coisas quando usa modularização

   String img = getClass().getResource( "images/cross-out.png").toExternalForm();
   burguer.setImage(new Image(img) ); 

   String cam = this.getClass().getResource( "css/interface.css").toExternalForm();
   burguer.getStyleClass().add(cam);
1 curtida

Fiz o que foi pedido.

Sobre a IDE: Minha IDE é o IntelliJ.

Removi o / antes do diretório, e o programa sequer iniciou. Normalmente ele inicia, mostrando uma Splash Screen antes de acusar o erro.

Por fim, tentei fazer o que pediu sobre a modularização, e o erro continua. Uma coisa que notei é que o sempre há erro acusando da variável root, em Controllers.

javafx.fxml.LoadException:
/C:/Users/lucas/IdeaProjects/LauncherNeed/out/production/LauncherNeed/sample/ui/dashboard.fxml:8

at javafx.fxml/javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2625)
at javafx.fxml/javafx.fxml.FXMLLoader.access$700(FXMLLoader.java:105)
at javafx.fxml/javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:943)
at javafx.fxml/javafx.fxml.FXMLLoader$InstanceDeclarationElement.processAttribute(FXMLLoader.java:980)
at javafx.fxml/javafx.fxml.FXMLLoader$Element.processStartElement(FXMLLoader.java:227)
at javafx.fxml/javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:752)
at javafx.fxml/javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2722)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2552)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2466)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3237)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3194)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3163)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3136)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3113)
at javafx.fxml/javafx.fxml.FXMLLoader.load(FXMLLoader.java:3106)
at LauncherNeed/sample.Controller$SplashScreen$1.run(Controller.java:48)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
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)
at java.base/java.lang.Thread.run(Thread.java:834)

Caused by: java.lang.IllegalAccessException: class javafx.fxml.FXMLLoader$ValueElement (in module javafx.fxml) cannot access class sample.ui.controllerDashboard (in module LauncherNeed) because module LauncherNeed does not export sample.ui to module javafx.fxml
at java.base/jdk.internal.reflect.Reflection.newIllegalAccessException(Reflection.java:361)
at java.base/jdk.internal.reflect.Reflection.ensureMemberAccess(Reflection.java:99)
at java.base/java.lang.Class.newInstance(Class.java:579)
at javafx.fxml/javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:936)
… 20 more
Exception in thread “JavaFX Application Thread” java.lang.NullPointerException: Root cannot be null
at javafx.graphics/javafx.scene.Scene.(Scene.java:345)
at javafx.graphics/javafx.scene.Scene.(Scene.java:207)
at LauncherNeed/sample.Controller$SplashScreen$1.run(Controller.java:54)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
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)
at java.base/java.lang.Thread.run(Thread.java:834)

Process finished with exit code 0

Ok,
Como está o module-info ?

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

opens sample;

}

    module <nome>  {
        requires javafx.fxml;
        requires javafx.graphics;
        requires javafx.controls;
        
        exports sample;    

        opens sample to javafx.fxml;
    }

Novamente outro erro:

javafx.fxml.LoadException:
/C:/Users/lucas/IdeaProjects/LauncherNeed/out/production/LauncherNeed/sample/ui/dashboard.fxml:8

at javafx.fxml/javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2625)
at javafx.fxml/javafx.fxml.FXMLLoader.access$700(FXMLLoader.java:105)
at javafx.fxml/javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:943)
at javafx.fxml/javafx.fxml.FXMLLoader$InstanceDeclarationElement.processAttribute(FXMLLoader.java:980)
at javafx.fxml/javafx.fxml.FXMLLoader$Element.processStartElement(FXMLLoader.java:227)
at javafx.fxml/javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:752)
at javafx.fxml/javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2722)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2552)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2466)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3237)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3194)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3163)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3136)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3113)
at javafx.fxml/javafx.fxml.FXMLLoader.load(FXMLLoader.java:3106)
at LauncherNeed/sample.Controller$SplashScreen$1.run(Controller.java:48)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
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)
at java.base/java.lang.Thread.run(Thread.java:834)

Caused by: java.lang.IllegalAccessException: class javafx.fxml.FXMLLoader$ValueElement (in module javafx.fxml) cannot access class sample.ui.controllerDashboard (in module LauncherNeed) because module LauncherNeed does not export sample.ui to module javafx.fxml
at java.base/jdk.internal.reflect.Reflection.newIllegalAccessException(Reflection.java:361)
at java.base/jdk.internal.reflect.Reflection.ensureMemberAccess(Reflection.java:99)
at java.base/java.lang.Class.newInstance(Class.java:579)
at javafx.fxml/javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:936)
… 20 more
Exception in thread “JavaFX Application Thread” java.lang.NullPointerException: Root cannot be null
at javafx.graphics/javafx.scene.Scene.(Scene.java:345)
at javafx.graphics/javafx.scene.Scene.(Scene.java:207)
at LauncherNeed/sample.Controller$SplashScreen$1.run(Controller.java:54)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
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)
at java.base/java.lang.Thread.run(Thread.java:834)

O Que estou achando estranho é que sempre há alguns errinhos em comum, que são esses:

at LauncherNeed/sample.Controller$SplashScreen$1.run(Controller.java:48)

Exception in thread “JavaFX Application Thread” java.lang.NullPointerException: Root cannot be null

  at LauncherNeed/sample.Controller$SplashScreen$1.run(Controller.java:54)

Esse já é outro Erro

É o seguinte…, se desejar isola somente as partes principais, tipo a Splash Screen, o os fxml(s) e chama apenas um por vez e veja se traz, se não tiver conseguindo mesmo, posta pra mim Eu dou uma olhada no codigo.

Posso colocar no Google Drive e compartilhar o link contigo ?

Sim

Coloca o código fonte no github.

Ok, já vi o codigo aqui e o problema esta em liberar o sample.ui para javafx.fxml usar.

acrescenta a linha no module

module <nome>  {
        requires javafx.fxml;
        requires javafx.graphics;
        requires javafx.controls;
        
        exports sample;    

        opens sample to javafx.fxml;

        opens sample.ui to javafx.fml;   //  <--- coloca essa linha
    }

No entanto algumas boas praticas no uso do JavaFX.

1.Use Task em conjunto com a Thread.

2.Futuramente pode ser interessante você fazer uso do getController, então poderá criar dentro dele um metodo onde você passa a referencia de sua Aplicação principal, isso serve para as fxml(s) setar ou usar dados da Aplicação principal.

Bons Codigos

1 curtida

Este tópico foi resolvido ou não?

se vc ta com a mesma duvida, cria um tópico com os erros e posta o código q vc ta tendo problemas