应用程序启动方法javafx项目中的异常

时间:2019-03-19 03:17:48

标签: java javafx

每当我在FXML的根元素中用fx:controller =“ ...”指定控制器类时,或者尝试运行项目时,都会发生我的问题。 我的FXML文档:

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

<?import de.jensd.fx.glyphs.fontawesome.FontAwesomeIcon?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Menu?>
<?import javafx.scene.control.MenuBar?>
<?import javafx.scene.control.MenuItem?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.effect.DropShadow?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Text?>

<BorderPane stylesheets="@style.css" xmlns="http://javafx.com/javafx" fx:id="root" xmlns:fx="http://javafx.com/fxml" fx:controller="Controller.LoginController">
   <center>
      <VBox stylesheets="@style.css">
         <children>
            <AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" stylesheets="@style.css">
               <children>
                  <HBox alignment="CENTER" layoutX="14.0" layoutY="14.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
                     <padding>
                        <Insets bottom="20.0" left="20.0" right="20.0" top="20.0" />
                     </padding>
                     <children>
                        <Text strokeType="OUTSIDE" strokeWidth="0.0" style="-fx-font-family: Quicksand; -fx-font-weight: bolder; -fx-font-size: 2.4em; -fx-fill: white;" text="FIXIT LOGIN" wrappingWidth="199.99609375">
                           <effect>
                              <DropShadow />
                           </effect>
                        </Text>
                        <HBox alignment="CENTER_RIGHT" HBox.hgrow="ALWAYS">
                           <children>
                              <Button mnemonicParsing="false" style="-fx-background-color: transparent; -fx-text-fill: white; -fx-border-width: 0px 0px 2px 0px; -fx-border-color: black; -fx-font-size: 1.3em; -fx-font-weight: bold;" styleClass="-fx-text-fill" text="Signup">
                                 <HBox.margin>
                                    <Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
                                 </HBox.margin>
                              </Button>
                              <Button mnemonicParsing="false" style="-fx-background-color: transparent; -fx-text-fill: #aeaeae; -fx-border-width: 0px 0px 2px 0px; -fx-border-color: black; -fx-font-size: 1.3em; -fx-font-weight: bold;" styleClass="-fx-text-fill" text="Login">
                                 <HBox.margin>
                                    <Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
                                 </HBox.margin>
                              </Button>
                           </children>
                        </HBox>
                     </children>
                  </HBox>
                  <VBox layoutX="175.0" layoutY="100.0" prefHeight="200.0" prefWidth="250.0">
                     <children>
                        <HBox alignment="CENTER" style="-fx-border-color: black; -fx-border-width: 0px 0px 2px 0px;">
                           <children>
                              <FontAwesomeIcon iconName="USER" size="1.3em" />
                              <TextField prefHeight="40.0" promptText="USERNAME" style="-fx-background-color: transparent; -fx-prompt-text-fill: black; -fx-border-width: 0px 0px 2px 0px;" HBox.hgrow="ALWAYS">
                                 <HBox.margin>
                                    <Insets top="20.0" />
                                 </HBox.margin>
                              </TextField>
                           </children>
                        </HBox>
                        <HBox alignment="CENTER" style="-fx-border-color: black; -fx-border-width: 0px 0px 2px 0px;">
                           <children>
                              <FontAwesomeIcon iconName="KEY" size="1.3em" />
                              <TextField accessibleRole="PASSWORD_FIELD" prefHeight="40.0" promptText="PASSWORD" style="-fx-background-color: transparent; -fx-prompt-text-fill: black;" HBox.hgrow="ALWAYS">
                                 <HBox.margin>
                                    <Insets top="20.0" />
                                 </HBox.margin>
                              </TextField>
                           </children>
                        </HBox>
                        <Button mnemonicParsing="false" prefHeight="25.0" prefWidth="252.0" style="-fx-background-color: #2f2f2fa3; -fx-background-radius: 100; -fx-text-fill: white; -fx-font-family: Quicksand; -fx-font-weight: Bold;" text="  Login">
                           <VBox.margin>
                              <Insets top="20.0" />
                           </VBox.margin>
                           <graphic>
                              <FontAwesomeIcon fill="#f8f6f6" iconName="ANGLE_RIGHT" size="2em" />
                           </graphic>
                        </Button>
                     </children>
                  </VBox>
                  <HBox alignment="CENTER" layoutX="43.0" layoutY="300.0" prefHeight="83.0" prefWidth="600.0" style="-fx-background-color: #2f2f2fa3;" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0">
                     <children>
                        <VBox alignment="CENTER">
                           <children>
                              <Text strokeType="OUTSIDE" strokeWidth="0.0" style="-fx-font-size: 1.5em; -fx-font-family: Quicksand; -fx-fill: white; -fx-font-weight: bold;" text="FixIt Desktop App" />
                              <Text strokeType="OUTSIDE" strokeWidth="0.0" style="-fx-font-size: 1.5em; -fx-font-family: Quicksand; -fx-fill: white; -fx-font-weight: bold;" text="Fixit@esprit.tn" />
                              <Text strokeType="OUTSIDE" strokeWidth="0.0" style="-fx-font-size: 1.5em; -fx-font-family: Quicksand; -fx-fill: white; -fx-font-weight: bold;" text="27277228" />
                           </children>
                        </VBox>
                     </children>
                  </HBox>
               </children>
            </AnchorPane>
         </children>
         <padding>
            <Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
         </padding>
      </VBox>
   </center>
   <top>
      <MenuBar BorderPane.alignment="CENTER">
        <menus>
          <Menu mnemonicParsing="false" text="File">
            <items>
              <MenuItem mnemonicParsing="false" text="Close" />
            </items>
          </Menu>
          <Menu mnemonicParsing="false" text="Edit">
            <items>
              <MenuItem mnemonicParsing="false" text="Delete" />
            </items>
          </Menu>
          <Menu mnemonicParsing="false" text="Help">
            <items>
              <MenuItem mnemonicParsing="false" text="About" />
            </items>
          </Menu>
        </menus>
      </MenuBar>
   </top>
</BorderPane>

“我的控制器”位于名为“控制器”的程序包中,这是我的主类:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Core;

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

/**
 *
 * @author ASUS
 */
public class InterfaceLogin extends Application {

    @Override
    public void start(Stage stage) throws Exception {
       Parent root = FXMLLoader.load(this.getClass().getResource("../View/Login.fxml")); 
        Scene scene = new Scene(root);     
        stage.initStyle(StageStyle.TRANSPARENT);
        stage.setScene(scene);
        stage.show();
        stage.setResizable(false);

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

最后,这是我不断收到的错误

Exception in Application start method
java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    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(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
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(Thread.java:748)
Caused by: javafx.fxml.LoadException: Root hasn't been set. Use method setRoot() before load.
file:/C:/Users/ASUS/Documents/NetBeansProjects/FixItt/dist/run1115511378/FixItt.jar!/fixitt/Login.fxml:17

    at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2597)
    at javafx.fxml.FXMLLoader.access$100(FXMLLoader.java:103)
    at javafx.fxml.FXMLLoader$RootElement.constructValue(FXMLLoader.java:1326)
    at javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:746)
    at javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2707)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2527)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214)
    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 fixitt.InterfaceLogin.start(InterfaceLogin.java:24)
    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 fixitt.InterfaceLogin
Java Result: 1

请我需要您的帮助,自几个小时前以来,我一直在尝试解决此问题,但我一直遇到相同的错误。 编辑:这是我的项目结构: This is my project structure

0 个答案:

没有答案