Exception running application sample.Main

huangapple go评论48阅读模式
英文:

Exception running application sample.Main

问题

我正在使用JavaFX制作一个ToDo应用程序,但出现了以下错误(即使没有数据库,登录系统只是想简单运行它):

我喜欢编码,但当出现错误时我就不喜欢了,这就是为什么我被称为ErrorLane的原因。

Main.java:

package sample;

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

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception{
        Parent root = FXMLLoader.load(getClass().getResource("C:\\Users\\arhaan\\eclipse-workspace\\ToDoApp\\src\\sample\\viewlogin.fxml"));
        primaryStage.setTitle("Hello World");
        primaryStage.setScene(new Scene(root, 300, 275));
        primaryStage.show();
    }

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

login.fxml:

<?import javafx.geometry.Insets?>
<?import javafx.scene.layout.GridPane?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<GridPane fx:controller="sample.Controller"
          xmlns:fx="http://javafx.com/fxml" fx:controller="application.FXMLDocumentController" alignment="center" hgap="10" vgap="10">
</GridPane>

它在Eclipse而不是IntelliJ IDEA中使用JRE 1.8.0。

这是错误信息:

Exception in Application start method
java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
    at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at sun.launcher.LauncherHelper$FXHelper.main(Unknown Source)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$1(LauncherImpl.java:182)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NullPointerException: Location is required.
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3207)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
    at sample.Main.start(Main.java:13)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$8(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$7(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$5(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$6(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$4(WinApplication.java:186)
    ... 1 more
Exception running application sample.Main
英文:

I am making a ToDo App in JavaFX but it says this error (even without the database, login system just want to run it plainly):
I like to code but not when errors come up Thats why i am called ErrorLane

Main.java:

package sample;

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

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception{
        Parent root = FXMLLoader.load(getClass().getResource(&quot;C:\\Users\\arhaan\\eclipse-workspace\\ToDoApp\\src\\sample\\viewlogin.fxml&quot;));
        primaryStage.setTitle(&quot;Hello World&quot;);
        primaryStage.setScene(new Scene(root, 300, 275));
        primaryStage.show();
    }


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

login.fxml:

&lt;?import javafx.geometry.Insets?&gt;
&lt;?import javafx.scene.layout.GridPane?&gt;

&lt;?import javafx.scene.control.Button?&gt;
&lt;?import javafx.scene.control.Label?&gt;
&lt;?import javafx.scene.control.TextField?&gt;
&lt;GridPane fx:controller=&quot;sample.Controller&quot;
          xmlns:fx=&quot;http://javafx.com/fxml&quot; fx:controller=&quot;application.FXMLDocumentController&quot; alignment=&quot;center&quot; hgap=&quot;10&quot; vgap=&quot;10&quot;&gt;
&lt;/GridPane&gt;

It is in Eclipse not IntelliJ IDEA with JRE 1.8.0

This is the error:

Exception in Application start method
java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
    at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at sun.launcher.LauncherHelper$FXHelper.main(Unknown Source)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$1(LauncherImpl.java:182)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NullPointerException: Location is required.
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3207)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
    at sample.Main.start(Main.java:13)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$8(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$7(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$5(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$6(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$4(WinApplication.java:186)
    ... 1 more
Exception running application sample.Main

答案1

得分: 1

"error" 是由尝试将绝对路径用作FXMLoader参数而引起的NPE。FXMLoader需要一个URL,所以要将您的绝对路径转换为可传递给FXMLoader的URL,可以使用FileInputStream,或者转换为URL:

FileInputStream:

Parent root = FXMLLoader.load(new FileInputStream("C:\\Users\\arhaan\\eclipse-workspace\\ToDoApp\\src\\sample\\login.fxml"));

转换为URL:

Parent root = FXMLLoader.load(Paths.get("C:\\Users\\arhaan\\eclipse-workspace\\ToDoApp\\src\\sample\\login.fxml").toUri().toURL());

这回答了您的问题,但这是不良设计。请为您的源代码使用适当的结构,并将.fxml文件放在与控制器相同的包中的类路径中,要么直接与控制器一起,要么放在专用的资源目录中,该目录包含相同的包布局。使用硬编码的绝对路径,您的代码不具备可移植性,并且需要其他人在其系统上具有完全相同的路径才能运行您的代码。

英文:

The "error" is the NPE caused by trying to use an absolute path as argument to FXMLLoader. FXMLLoader takes a URL, so to convert your absolute path to a URL that can be passed to FXMLLoader, either use a FileInputStream, or convert to a URL:

FileInputStream:

 Parent root = FXMLLoader.load(new FileInputStream(&quot;C:\\Users\\arhaan\\eclipse-workspace\\ToDoApp\\src\\sample\\login.fxml&quot;));

Converting to URL:

Parent root = FXMLLoader.load(Paths.get(&quot;C:\\Users\\arhaan\\eclipse-workspace\\ToDoApp\\src\\sample\\login.fxml&quot;).toUri().toURL());

This answers your question, but this is bad design. Use proper structure for your source, and have the .fxml in the class path in the same package, either directly with the controller, or in a dedicated resource directory contains same package layout. With a hardcoded absolute path, your code is not portable, and will require anyone else to have the same exact path on their system to run your code.

huangapple
  • 本文由 发表于 2020年7月30日 13:25:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/63166687.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定