JavaFX проект с Maven - исключение в методе запуска - PullRequest
0 голосов
/ 22 января 2020

поэтому я создаю проект javafx, и все работало нормально, пока меня не заставили изменить его на проект maven, с которым я никогда не работал. Пожалуйста, помогите, я получаю следующую ошибку:

 Exception in Application start method
 Exception in thread "main" 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:567)
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:835)
Caused by: java.lang.NullPointerException: Location is required.
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3230)
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 sample.Main.switchScene(Main.java:42)
at sample.Main.start(Main.java:31)
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(AccessController.java:389)
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)

Вот часть кода, которая, я думаю, может быть связана с проблемой:

Main. java:

package sample;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import sample.views.login.LoginController;

import java.io.IOException;

public class Main extends Application {
public static final String WINDOW_TITLE = "Project";
public static final int WINDOW_WIDTH = 1200;
public static final int WINDOW_HEIGHT = 700;

public static Stage currentStage;

public static Scene previousScene;


@Override
public void start(Stage primaryStage) throws Exception {
    currentStage = primaryStage;

    currentStage.setTitle(WINDOW_TITLE);
    switchScene(LoginController.RESOURCE_MAIN_PATH);
    currentStage.show();
}

public static void switchScene(String resourcePath) {
    if (currentStage.getScene() != null) {
        previousScene = currentStage.getScene();
    }

    try {
        Parent root = FXMLLoader.load(Main.class.getResource(resourcePath));
        Scene scene = new Scene(root, Main.WINDOW_WIDTH, Main.WINDOW_HEIGHT);

        currentStage.setScene(scene);
    } catch (IOException e) {
        e.printStackTrace();
    }
}
}

LoginController. java:

package sample.views.login;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import sample.Controller;
import sample.Main;

public class LoginController {
    public static final String RESOURCE_MAIN_PATH = "views/login/login.fxml";
    private Label isConnected;
    @FXML
    private TextField input_login;
    @FXML
    private PasswordField input_password;
    @FXML
    private Button button_submit;

    @FXML
    private void handleSubmitAction(ActionEvent event) {
                Main.switchScene(Controller.RESOURCE_PATH);
    }
}

login.f xml:

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

<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<GridPane fx:controller="sample.views.login.LoginController" stylesheets="@login.css"
      xmlns:fx="http://javafx.com/fxml" xmlns="http://javafx.com/javafx" alignment="CENTER">
    <VBox GridPane.rowIndex="0" GridPane.columnIndex="0" spacing="32" alignment="CENTER">

        <Label  text="Hello" id="title_label"/>

        <VBox spacing="8">
            <VBox spacing="2">
                <Label text="Login:"/>
                <TextField fx:id="input_login" styleClass="input"/>
            </VBox>

            <VBox spacing="2">
                <Label text="Hasło:"/>
                <PasswordField fx:id="input_password" styleClass="input"/>
            </VBox>
        </VBox>

        <Button text="Zaloguj" fx:id="button_submit" styleClass="button" onAction="#handleSubmitAction"/>
    </VBox>
</GridPane>

pom. xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>groupId</groupId>
<artifactId>test</artifactId>
<dependencies>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>8.0.19</version>
    </dependency>
</dependencies>
<version>1.0-SNAPSHOT</version>
<properties>
    <maven.compiler.release>11</maven.compiler.release>
</properties>

<build>
    <pluginManagement>
        <plugins>
            <plugin>

                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
            </plugin>
        </plugins>
    </pluginManagement>
</build>

</project>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...