JavaFX.fxml.LoadException

JavaFX.fxml.LoadException,javafx,netbeans,builder,scene,Javafx,Netbeans,Builder,Scene,我是javafx新手,尝试用javafx制作一个功能性登录应用程序。当我运行netbeans for javafx fxml应用程序的默认代码时,它运行得非常完美,但当我使用场景生成器和代码简单逻辑创建自己的应用程序时,它会给出错误 这是错误所在 Executing C:\Users\Ovonel\Documents\NetBeansProjects\Digi_Library\dist\run1053353439\Digi_Library.jar using platform C:\Progra

我是javafx新手,尝试用javafx制作一个功能性登录应用程序。当我运行netbeans for javafx fxml应用程序的默认代码时,它运行得非常完美,但当我使用场景生成器和代码简单逻辑创建自己的应用程序时,它会给出错误

这是错误所在

Executing C:\Users\Ovonel\Documents\NetBeansProjects\Digi_Library\dist\run1053353439\Digi_Library.jar using platform C:\Program Files (x86)\Java\jdk1.8.0_172\jre/bin/java
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: 
file:/C:/Users/Ovonel/Documents/NetBeansProjects/Digi_Library/dist/run1053353439/Digi_Library.jar!/digi_library/login/Login.fxml:11

    at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2579)
    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 digi_library.login.Login.start(Login.java:15)
    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
Caused by: java.lang.NullPointerException
    at digi_library.login.LoginController.<init>(LoginController.java:17)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at java.lang.Class.newInstance(Class.java:442)
    at sun.reflect.misc.ReflectUtil.newInstance(ReflectUtil.java:51)
    at javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:927)
    at javafx.fxml.FXMLLoader$InstanceDeclarationElement.processAttribute(FXMLLoader.java:971)
    at javafx.fxml.FXMLLoader$Element.processStartElement(FXMLLoader.java:220)
    at javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:744)
    at javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2707)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2527)
    ... 17 more
Exception running application digi_library.login.Login
Java Result: 1
这是Fxml文件

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

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.PasswordField?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.Pane?>


<Pane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="310.0" prefWidth="400.0" xmlns="http://javafx.com/javafx/9.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="digi_library.login.LoginController">
   <children>
      <PasswordField layoutX="100.0" layoutY="180.0" prefHeight="25.0" prefWidth="200.0" promptText="Password" />
      <TextField layoutX="100.0" layoutY="115.0" prefHeight="25.0" prefWidth="200.0" promptText="Username" />
      <ImageView fitHeight="40.0" fitWidth="40.0" layoutX="180.0" layoutY="14.0" pickOnBounds="true" preserveRatio="true">
         <image>
            <Image url="@../../resources/icons/icons8_Lock_100px.png" />
         </image>
      </ImageView>
      <Button layoutX="50.0" layoutY="256.0" mnemonicParsing="false" prefHeight="30.0" prefWidth="100.0" text="Login" />
      <Button layoutX="250.0" layoutY="256.0" mnemonicParsing="false" prefHeight="30.0" prefWidth="100.0" text="Cancel" />
      <ImageView fitHeight="20.0" fitWidth="20.0" layoutX="65.0" layoutY="118.0" pickOnBounds="true" preserveRatio="true">
         <image>
            <Image url="@../../resources/icons/icons8_User_24px.png" />
         </image>
      </ImageView>
      <ImageView fitHeight="20.0" fitWidth="20.0" layoutX="65.0" layoutY="183.0" pickOnBounds="true" preserveRatio="true">
         <image>
            <Image url="@../../resources/icons/icons8_Password_1_24px.png" />
         </image>
      </ImageView>
   </children>
</Pane>

我在场景生成器中尝试了fx:root选项,但没有成功。当我在项目中使用jfoenix.jar时,也会出现这个错误。任何帮助都将不胜感激

看一看这张照片。您需要使用控制器中的
@FXML
和FXML中的
fx:id
将文本字段注入控制器。您需要将
用户名
密码
的初始化移动到事件处理程序(或仅在那里声明它们):您需要在用户键入时获取它们的文本,而不是在首次创建它们时。离题:不要混合使用不同的UI工具包。使用或,而不是
JOptionPane
(这是一个swing组件)。尝试在FX应用程序线程上显示
JOptionPane
,将在许多平台上挂起应用程序。顺便问一下,我使用的是JDK 8,它是否与javafx有关?非常感谢。程序运行得很好!JavaSwing应用程序很简单……javafx有点棘手。再次感谢!
    <?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.PasswordField?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.Pane?>


<Pane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="310.0" prefWidth="400.0" xmlns="http://javafx.com/javafx/9.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="digi_library.login.LoginController">
   <children>
      <PasswordField layoutX="100.0" layoutY="180.0" prefHeight="25.0" prefWidth="200.0" promptText="Password" />
      <TextField layoutX="100.0" layoutY="115.0" prefHeight="25.0" prefWidth="200.0" promptText="Username" />
      <ImageView fitHeight="40.0" fitWidth="40.0" layoutX="180.0" layoutY="14.0" pickOnBounds="true" preserveRatio="true">
         <image>
            <Image url="@../../resources/icons/icons8_Lock_100px.png" />
         </image>
      </ImageView>
      <Button layoutX="50.0" layoutY="256.0" mnemonicParsing="false" prefHeight="30.0" prefWidth="100.0" text="Login" />
      <Button layoutX="250.0" layoutY="256.0" mnemonicParsing="false" prefHeight="30.0" prefWidth="100.0" text="Cancel" />
      <ImageView fitHeight="20.0" fitWidth="20.0" layoutX="65.0" layoutY="118.0" pickOnBounds="true" preserveRatio="true">
         <image>
            <Image url="@../../resources/icons/icons8_User_24px.png" />
         </image>
      </ImageView>
      <ImageView fitHeight="20.0" fitWidth="20.0" layoutX="65.0" layoutY="183.0" pickOnBounds="true" preserveRatio="true">
         <image>
            <Image url="@../../resources/icons/icons8_Password_1_24px.png" />
         </image>
      </ImageView>
   </children>
</Pane>
package digi_library.login;

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


public class Login extends Application {

    @Override
    public void start(Stage stage) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource("Login.fxml"));

        Scene scene = new Scene(root);

        stage.setScene(scene);
        stage.show();
    }


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

}