尝试设置文本字段文本(Javafx)时出错

尝试设置文本字段文本(Javafx)时出错,java,javafx,Java,Javafx,已解决-请参阅底部的解决方案 在启动我正在使用的桌面应用程序时,我正在尝试从.xml文件加载电子邮件和密码 这个想法是,用户可以在第一次登录时单击“记住我”复选框,这将写入一个新的.xml文件,其中包含他们的电子邮件和密码 下次他们打开应用程序时,他们的电子邮件和密码应该已经加载到电子邮件文本字段和密码字段中 一切正常,直到我试图设置这两个字段的文本时,在启动时出现错误 如果我不尝试设置text/password字段,而只是测试用.xml文件中的数据填充的变量,结果与预期的一样 这是我的控制器代

已解决-请参阅底部的解决方案

在启动我正在使用的桌面应用程序时,我正在尝试从.xml文件加载电子邮件和密码

这个想法是,用户可以在第一次登录时单击“记住我”复选框,这将写入一个新的.xml文件,其中包含他们的电子邮件和密码

下次他们打开应用程序时,他们的电子邮件和密码应该已经加载到电子邮件文本字段和密码字段中

一切正常,直到我试图设置这两个字段的文本时,在启动时出现错误

如果我不尝试设置text/password字段,而只是测试用.xml文件中的数据填充的变量,结果与预期的一样

这是我的控制器代码:

public class LogInController{

@FXML
private VBox vbox;

private Parent fxml;

@FXML
private TextField signUpFullName, signUpEmail, signUpPassword;

@FXML
public TextField signInEmail;

@FXML
private PasswordField signInPassword;

@FXML
private Label notificationMessage, notificationMessageLogin;

@FXML
private JFXButton SignInButton, ForgotPasswordButton;

@FXML
private CheckBox rememberMeCheckbox;

private PasswordEncryptionService encryption;
private String fullName = "";
private String email = "";

private int count;
private final String xmlFilePath = "C:\\Users\\konim\\IdeaProjects\\Momentum\\src\\sample\\rememberme.xml";
private String userEmail ="";
private String password;
private String savedEmail;
private String savedPassword;
private String savedSalt;
private String savedSecuredPassword;
private boolean isEmailRegistered;

public void initialize() throws Exception {
    TranslateTransition t = new TranslateTransition(Duration.seconds(0.5), vbox);
    t.setToX(340);
    t.play();
    t.setOnFinished((e) -> {
        try {
            fxml = FXMLLoader.load(getClass().getResource("fxml/SignInVBox.fxml"));
            vbox.getChildren().removeAll();
            vbox.getChildren().setAll(fxml);
        } catch (IOException e1) {
            e1.printStackTrace();
        }
    });

    File file = new File("C:\\Users\\konim\\IdeaProjects\\Momentum\\src\\sample\\rememberme.xml");
    boolean exists = file.exists();
    if (exists) {
        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
        Document document = documentBuilder.parse(file);
        document.getDocumentElement().normalize();
        savedEmail = document.getElementsByTagName("Email").item(0).getTextContent();
        savedPassword = document.getElementsByTagName("Password").item(0).getTextContent();
        if (savedEmail.length() >= 1 && savedPassword.length() >1) {
            System.out.println(savedEmail); // TEST and value is correct
            System.out.println(savedPassword); // TEST and value is correct
        }

    }
}
现在,如果我在最后一个If语句中添加以下两行,我会得到下面的错误

signInEmail.setText(savedEmail);
signInPassword.setText(savedPassword);
错误

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: /C:/Users/konim/IdeaProjects/Momentum/out/production/Momentum/sample/fxml/LogInScene.fxml

at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2571)
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 sample.Main.start(Main.java:14)
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.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 sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2566)
... 17 more
Caused by: java.lang.NullPointerException
at sample.LogInController.initialize(LogInController.java:101)
... 27 more
Exception running application sample.Main
如果我不加上这两行,就不会有错误,一切都会顺利进行

你能帮我找到哪里不对劲吗?尝试查找过去两个小时的错误消息,但没有成功

我的FXML代码

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

<?import com.jfoenix.controls.JFXButton?>
<?import de.jensd.fx.glyphs.fontawesome.FontAwesomeIconView?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.CheckBox?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.PasswordField?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>

    <VBox id="VBox" alignment="CENTER_LEFT" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="480.0" prefWidth="480.0" styleClass="white-pane" stylesheets="@../css/stylesheet.css" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.LogInController">
       <children>
          <Label text="Sign In" textFill="#f78c7b">
             <font>
                <Font size="20.0" />
             </font>
             <VBox.margin>
                <Insets left="20.0" right="15.0" top="20.0" />
             </VBox.margin>
          </Label>
          <HBox alignment="CENTER_LEFT" prefHeight="45.0" prefWidth="200.0" styleClass="tf_box">
             <VBox.margin>
                <Insets left="15.0" right="15.0" top="20.0" />
             </VBox.margin>
             <children>
                <TextField fx:id="signInEmail" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" promptText="Email" styleClass="tf" HBox.hgrow="ALWAYS">
                   <font>
                      <Font size="15.0" />
                   </font>
                </TextField>
                <FontAwesomeIconView fill="#dedee4" glyphName="ENVELOPE" size="1.5em" />
             </children>
          </HBox>
          <HBox alignment="CENTER_LEFT" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" styleClass="tf_box">
             <children>
                <PasswordField fx:id="signInPassword" prefHeight="25.0" prefWidth="436.0" promptText="Password" styleClass="tf">
                   <font>
                      <Font size="15.0" />
                   </font>
                </PasswordField>
                <FontAwesomeIconView fill="#dedee4" glyphName="LOCK" size="1.5em" />
             </children>
             <VBox.margin>
                <Insets left="15.0" right="15.0" top="20.0" />
             </VBox.margin>
          </HBox>
          <HBox alignment="CENTER_RIGHT" prefHeight="45.0" prefWidth="200.0">
             <VBox.margin>
                <Insets left="15.0" right="15.0" top="20.0" />
             </VBox.margin>
             <children>
                <JFXButton fx:id="ForgotPasswordButton" maxWidth="1.7976931348623157E308" onAction="#actionForgotPassword" text="Forgot Password?" textFill="#868686" HBox.hgrow="ALWAYS">
                   <font>
                      <Font size="15.0" />
                   </font>
                   <HBox.margin>
                      <Insets right="20.0" />
                   </HBox.margin>
                </JFXButton>
                <CheckBox fx:id="rememberMeCheckbox" mnemonicParsing="false" text="Remember me" textFill="#868686">
                   <HBox.margin>
                      <Insets right="20.0" />
                   </HBox.margin>
                   <font>
                      <Font size="15.0" />
                   </font>
                </CheckBox>
                <JFXButton fx:id="SignInButton" onAction="#actionSignIn" prefHeight="39.0" prefWidth="120.0" ripplerFill="WHITE" styleClass="pink-btn" text="Sign In">
                   <font>
                      <Font size="15.0" />
                   </font>
                </JFXButton>
             </children>
          </HBox>
          <HBox alignment="CENTER" prefHeight="100.0" prefWidth="200.0">
             <children>
                <Label fx:id="notificationMessageLogin" alignment="CENTER" prefHeight="88.0" prefWidth="456.0">
                   <font>
                      <Font name="Calibri Italic" size="14.0" />
                   </font>
                </Label>
             </children>
          </HBox>
       </children>
    </VBox>
现在,我没有使用三个不同的场景,而是将它们全部添加在一起,并在触发动作触发器时在两个vBox上使用.setVisible(true/false)

我不知道问题的确切原因,但至少现在已经解决了。我猜这与我调用一个新场景的方式有关,因为它们都使用同一个控制器,这在我脑海中意味着它试图从控制器内部调用控制器,有点递归

如果有人能澄清我的理论是否正确,我愿意学习


谢谢。

我想signInEmail没有初始化。请仔细检查类包fx:controller=“sample.LogInController”。我遗漏了一些我认为不需要的代码,但我已经更新了上面的代码以包含这些代码。这只是.fxml文件的一些加载/转换。我还没有建造任何东西。只是在IntelliJ中运行代码?如果这就是你的意思:-)编程才几个月,所以还有很多外国单词需要我学习。克利奥帕特拉,谢谢你帮助我提高!我试过了,它对我有效。嗯,我不得不添加两个额外的动作处理程序(从ST开始,我想你已经准备好了它们),还将jfxbuttons更改为普通按钮,并删除了fontawesome(因为我手头没有)。但是随着对话框的出现,我可以设置相应的字段。所以我倾向于认为fxml有问题——我认为它拉错了东西。我很确定你发布的控制器代码中没有。这让我更加恼火:-)我会尝试再次检查fxml代码。sry会破坏你的一天。。但这不是一个解决方案,它只是掩盖了问题;)这似乎是因为某种原因,这两个字段没有被注入-你必须找到这个原因。在一轮调试中,自上而下:剥离零件直到问题消失,或者自下而上:从一个最小的示例开始,添加零件直到问题出现。快乐狩猎:)我猜signInEmail没有初始化。请仔细检查类包fx:controller=“sample.LogInController”。我遗漏了一些我认为不需要的代码,但我已经更新了上面的代码以包含这些代码。这只是.fxml文件的一些加载/转换。我还没有建造任何东西。只是在IntelliJ中运行代码?如果这就是你的意思:-)编程才几个月,所以还有很多外国单词需要我学习。克利奥帕特拉,谢谢你帮助我提高!我试过了,它对我有效。嗯,我不得不添加两个额外的动作处理程序(从ST开始,我想你已经准备好了它们),还将jfxbuttons更改为普通按钮,并删除了fontawesome(因为我手头没有)。但是随着对话框的出现,我可以设置相应的字段。所以我倾向于认为fxml有问题——我认为它拉错了东西。我很确定你发布的控制器代码中没有。这让我更加恼火:-)我会尝试再次检查fxml代码。sry会破坏你的一天。。但这不是一个解决方案,它只是掩盖了问题;)这似乎是因为某种原因,这两个字段没有被注入-你必须找到这个原因。在一轮调试中,自上而下:剥离零件直到问题消失,或者自下而上:从一个最小的示例开始,添加零件直到问题出现。快乐狩猎:)
fxml = FXMLLoader.load(getClass().getResource("fxml/SignInVBox.fxml"));
vbox.getChildren().removeAll();
vbox.getChildren().setAll(fxml);