客户端/服务器jar-线程“主”中的异常java.lang.reflect.InvocationTargetException

时间:2019-05-15 21:14:11

标签: java javafx client-server

我在Eclipse中有一个Javafx项目可以运行。我有一个客户端和一个服务器。 我试图在eclipse中运行服务器和客户端,并且一切正常,但是当我导出到可运行的jar,一个导出到服务器,一个导出到客户端时,服务器jar运行正常,但是客户端抛出异常:线程“ main” java中出现异常.lang.reflect.InvocationTargetException

尽管在导出日食前工作正常。 服务器和客户端都是以GUI开头的javafx应用程序。

ClientLauncher.java:

package client;

import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.input.MouseEvent;
import javafx.stage.Stage;

public class ClientLauncher extends Application {

    public GCMClient gcmClient;

    private String host = "localhost";
    private int port = 5555;

    private double xOffset = 0;
    private double yOffset = 0;

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

    }

    @Override
    public void start(Stage PrimaryStage) throws Exception {

        System.out.println("Client Connection Established");
        Parent root = FXMLLoader.load(getClass().getResource("/sources/ServerLogin.fxml"));
        root.setOnMousePressed(new EventHandler<MouseEvent>() {

            public void handle(MouseEvent event) {
                xOffset = event.getSceneX();
                yOffset = event.getSceneY();
            }
        });
        root.setOnMouseDragged(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                PrimaryStage.setX(event.getScreenX() - xOffset);
                PrimaryStage.setY(event.getScreenY() - yOffset);
            }
        });

        Scene scene = new Scene(root);
        PrimaryStage.setScene(scene);
        PrimaryStage.show();
    }

}

我得到的异常:

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(Unknown Source)
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.base/java.lang.reflect.Method.invoke(Unknown Source)
        at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:58)
Caused by: java.lang.RuntimeException: Exception in Application start method
        at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(Unknown Source)
        at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(Unknown Source)
        at java.base/java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NullPointerException: Location is required.
        at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
        at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
        at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
        at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
        at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
        at javafx.fxml/javafx.fxml.FXMLLoader.load(Unknown Source)
        at client.ClientLauncher.start(ClientLauncher.java:33)
        at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(Unknown Source)
        at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$11(Unknown Source)
        at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$9(Unknown Source)
        at java.base/java.security.AccessController.doPrivileged(Native Method)
        at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(Unknown Source)
        at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(Unknown Source)
        at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
        at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(Unknown Source)

1 个答案:

答案 0 :(得分:0)

已解决:

我在

中拼错了ServerLogin.fxml
 Parent root = FXMLLoader.load(getClass().getResource("/sources/ServerLogin.fxml"));

在我的案例中,这被命名为“ ServerLogIn.fxml”。

在可运行的jar中,fxml的名称似乎区分大小写,但是在eclipse中却不区分大小写。这就是为什么它在eclipse中可以正常工作,而在jar中则不能工作的原因。