当前位置: 首页 > 知识库问答 >
问题:

Java外汇错误-运行应用程序示例时出现异常。main[重复]

徐旻
2023-03-14

我正试图修复这个错误...但似乎什么都不起作用。在我插入到“www.javafx.com”sample.fxml的超链接之前,应用程序一直运行

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

<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<?import javafx.scene.control.skin.SplitPaneSkin.Content?>
<?import javafx.scene.web.WebView?>
<GridPane alignment="center" hgap="10" vgap="10" xmlns="http://javafx.com/javafx/11.0.1"
          xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller" stylesheets="@style.css"
          fx:id="gridPane">
        <Button text="Open.." fx:id="open" onAction="#handleClick" GridPane.columnIndex="1" GridPane.rowIndex="1"/>
        <Button text="Button " fx:id="two" GridPane.columnIndex="2" GridPane.rowIndex="1"/>
        <Button text="Button " fx:id="three" GridPane.columnIndex="3" GridPane.rowIndex="1"/>
        <Button text="Button " fx:id="four" GridPane.columnIndex="4" GridPane.rowIndex="1"/>
        <Button text="Button " fx:id="five" GridPane.columnIndex="5" GridPane.rowIndex="1"/>
        <Hyperlink text = "www.javafx.com"  GridPane.rowIndex="3" GridPane.columnIndex="0" onAction="#handleLinkClick" />
        <WebView  GridPane.rowIndex="4" GridPane.columnIndex="0"  GridPane.columnSpan="5" GridPane.rowSpan="3"/>
</GridPane> ```

**Main is :**
```public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception {
        FXMLLoader loader = new FXMLLoader(new File("/Application/sample.fxml").toURI().toURL());
        Parent root = loader.load();
        primaryStage.setTitle("Test");
        primaryStage.setScene(new Scene(root, 600, 275));
        primaryStage.show();
;
    }
    public static void main(String[] args) {
        launch(args);
    }
}
```

配置了SDK,设置了VM选项,我从javafx导入了所有的包。我还设置了环境变量。所有设置似乎正常..错误是:

java.lang.reflect.InvocationTargetException
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:64)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:564)
    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:64)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:564)
    at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1071)
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:832)
Caused by: java.io.FileNotFoundException: D:\Application\sample.fxml (The system cannot find the path specified)
    at java.base/java.io.FileInputStream.open0(Native Method)
    at java.base/java.io.FileInputStream.open(FileInputStream.java:211)
    at java.base/java.io.FileInputStream.<init>(FileInputStream.java:153)
    at java.base/java.io.FileInputStream.<init>(FileInputStream.java:108)
    at java.base/sun.net.www.protocol.file.FileURLConnection.connect(FileURLConnection.java:86)
    at java.base/sun.net.www.protocol.file.FileURLConnection.getInputStream(FileURLConnection.java:189)
    at java.base/java.net.URL.openStream(URL.java:1167)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2547)
    at javafx.fxml/javafx.fxml.FXMLLoader.load(FXMLLoader.java:2517)
    at JavaFX/sample.Main.start(Main.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:474)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:447)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:446)
    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 sample.Main ```

共有1个答案

白晋鹏
2023-03-14

而不是写

FXMLLoader loader = new FXMLLoader(new File("/Application/sample.fxml").toURI().toURL());
        Parent root = loader.load();

试试看

Parent root = FXMLLoader.load(getClass().getResource("nameOfFxml.fxml"));
 类似资料: