Exception in Application start method. JavaFx 11

佐手、 提交于 2021-01-28 08:05:19

问题


I try to create a screen with one TextField and Button and receve "Exception in Application start method". Firt time I try solution from this questions and dont work:

Exception in Application start method

Exception in Application start method javafx gui

JavaFX "Location is required." even though it is in the same package

Exception in Application start method error. Other solutions not working

I use Java 11, javaFx 11. For javaFx I use maven.

The main class is :

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

public class StartClass extends Application{
    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage stage) throws Exception  {
        Parent parent =     FXMLLoader.load(getClass().getResource("../userInterface/CreateProjectScreen.fxml"));
        stage.setTitle("IEPCP");
        stage.setScene(new Scene(parent ));
        stage.show();
    }

}

The FXML file is:

<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>

<AnchorPane prefHeight="400.0" prefWidth="600.0"     xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1"     fx:controller="controllers.ui.CreateProjectController">
   <children>
      <AnchorPane prefHeight="402.0" prefWidth="601.0">
         <children>
            <Button fx:id="sendButton" layoutX="249.0" layoutY="310.0" mnemonicParsing="false" onAction="#sendButtonClicked" text="Button" />
            <TextField fx:id="directory" layoutX="66.0" layoutY="85.0" prefHeight="25.0" prefWidth="470.0" />
         </children>
      </AnchorPane>
   </children>
</AnchorPane>

Controller class:

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

public class CreateProjectController {

    @FXML
    private TextField directory;

    @FXML
    private Button sendButton;

    public TextField getDirectory() {
        return directory;
    }

    public void setDirectory(TextField directory) {
        this.directory = directory;
    }

    public void sendButtonClicked(ActionEvent actionEvent){
        System.out.println("You write: " + getDirectory().getText());
    }
}

This is error receved:

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:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    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:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1051)
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:834)
Caused by: java.lang.IllegalAccessError: class com.sun.javafx.fxml.FXMLLoaderHelper (in unnamed module @0x3e122bd1) cannot access class com.sun.javafx.util.Utils (in module javafx.graphics) because module javafx.graphics does not export com.sun.javafx.util to unnamed module @0x3e122bd1
    at com.sun.javafx.fxml.FXMLLoaderHelper.<clinit>(FXMLLoaderHelper.java:38)
    at javafx.fxml.FXMLLoader.<clinit>(FXMLLoader.java:2056)
    at primary.StartClass.start(StartClass.java:17)
    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:455)
    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)
    ... 1 more
Exception running application primary.StartClass

Maven dependencies:

<dependencies>
    <dependency>
        <groupId>org.openjfx</groupId>
        <artifactId>javafx-controls</artifactId>
        <version>11</version>
    </dependency>
    <dependency>
        <groupId>org.openjfx</groupId>
        <artifactId>javafx-fxml</artifactId>
        <version>11</version>
    </dependency>
</dependencies>

回答1:


The issue is to do with the path that you are providing to your FXMLLoader. If your FXML file is in src/main/resources/userInterface/CreateProjectScreen.fxml, then your FXMLLoader should be called as follows:

Parent parent =     FXMLLoader.load(getClass().getResource("/userInterface/CreateProjectScreen.fxml"));

The leading slash refers to the root of the classpath (i.e. src/main/resources). The path following that is the relative path to the FXML file.

You are also getting a module access error. You need to export the module listed in the error message, otherwise JavaFX will not be able to access it.



来源:https://stackoverflow.com/questions/57221488/exception-in-application-start-method-javafx-11

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!