我在运行我的javafx文件时遇到了问题,我是javafx的新手。所以我想知道为什么以及如何修复这个错误。fxml文件只是我在过去一年的一些任务中做的一个小实验。所以我希望有人能帮我找出为什么我的程序不能运行。
我的view class package view;
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{
private Stage stage;
public static void main(String args []) {
launch(args);
}
@Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("Main.fxml"));
Scene scene = new Scene(root);
stage.setTitle("Main Menu");
stage.setScene(scene);
stage.show();
}
}我的fxml生成的代码
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.effect.ColorInput?>
<?import javafx.scene.effect.Glow?>
<?import javafx.scene.effect.Light.Distant?>
<?import javafx.scene.effect.Lighting?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>
<AnchorPane xmlns="http://javafx.com/javafx/9.0.1"
xmlns:fx="http://javafx.com/fxml/1">
<children>
<AnchorPane prefHeight="562.0" prefWidth="592.0">
<children>
<Button layoutX="32.0" layoutY="73.0" mnemonicParsing="false" prefHeight="61.0" prefWidth="245.0" text="Session Maintenance">
<font>
<Font size="18.0" />
</font>
</Button>
<Button layoutX="314.0" layoutY="167.0" mnemonicParsing="false" prefHeight="61.0" prefWidth="245.0" text="Apply Membership">
<font>
<Font size="18.0" />
</font>
</Button>
<Button layoutX="314.0" layoutY="73.0" mnemonicParsing="false" prefHeight="61.0" prefWidth="245.0" text="Seating Maintenance">
<font>
<Font size="18.0" />
</font>
</Button>
<Button layoutX="32.0" layoutY="167.0" mnemonicParsing="false" prefHeight="61.0" prefWidth="245.0" text="Ticketing Maintenance">
<font>
<Font size="18.0" />
</font>
</Button>
<Button layoutX="174.0" layoutY="262.0" mnemonicParsing="false" prefHeight="61.0" prefWidth="245.0" text="Exit">
<font>
<Font size="18.0" />
</font>
</Button>
<ImageView fitHeight="212.0" fitWidth="245.0" layoutX="14.0" layoutY="336.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@JS.png" />
</image>
</ImageView>
<TextField alignment="CENTER" layoutX="251.0" layoutY="492.0" prefHeight="56.0" prefWidth="327.0" text="Customer Hotline - 0199660803 / 0172599389">
<effect>
<Lighting diffuseConstant="1.37" specularConstant="0.37" specularExponent="40.0" surfaceScale="0.8">
<bumpInput>
<ColorInput />
</bumpInput>
<light>
<Light.Distant color="#33dbf2" />
</light>
</Lighting>
</effect>
</TextField>
</children>
<effect>
<Glow level="0.0" />
</effect>
</AnchorPane>
<TextField alignment="CENTER" prefHeight="0.0" prefWidth="592.0" text="Welcome to JS Rocket Systems">
<font>
<Font size="26.0" />
</font>
</TextField>
</children>
</AnchorPane>提示的错误
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$154(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 view.Main.start(Main.java:21)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$161(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$174(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(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$147(WinApplication.java:177)
... 1 more
Exception running application view.Main有没有人能告诉我如何解决这个问题,以及如何让它运行?谢谢!
发布于 2018-04-21 22:21:45
验证fxml文件的名称和URL是否正确
Parent root = FXMLLoader.load(getClass().getResource("/view/Main.fxml"));发布于 2018-10-21 07:12:48
从fxml文件删除时生成错误
<light>
<Light.Distant color="#33dbf2" />
</light>

从XML文件中删除该行之后。

https://stackoverflow.com/questions/49956610
复制相似问题