Javafx Scene Builder项目未运行(提示运行时错误)

时间:2018-04-21 14:07:29

标签: java eclipse javafx scenebuilder

我在运行javafx文件时遇到问题,我对javafx很新。所以我想知道为什么以及如何解决这个错误。 fxml文件只是我过去一年的一些分配实验。所以我希望有人帮我弄清楚我的程序没有运行的原因。

我的观点课 包视图;

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

有人可以告诉我如何解决这个问题以及如何让它运行?谢谢!

2 个答案:

答案 0 :(得分:0)

验证fxml文件的名称和URL(如果正确)

Parent root = FXMLLoader.load(getClass().getResource("/view/Main.fxml"));

答案 1 :(得分:-1)

从fxml文件中退出产生了错误

<div class="container">
    <div class="row">
        <div class="col-lg-10 col-lg-1-offset">
            <form action="." method="post" enctype="multipart/form-data">
                {% csrf_token %}
                {{ form.as_p }}
                <input type="submit" value="Submit" />
            </form>

        </div>
    </div>
</div>

enter image description here

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

enter image description here