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

时间:2019-02-25 16:23:42

标签: java javafx

已解决-查看底部的解决方案

我正在尝试在正在处理的桌面应用程序启动时从.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>

解决方案

在我拥有3个场景之前-主登录场景和两个vBox场景在初始化时被加载到了主登录场景中,或者在过渡后进行了更改。

我在过渡中使用了以下代码:

fxml = FXMLLoader.load(getClass().getResource("fxml/SignInVBox.fxml"));
vbox.getChildren().removeAll();
vbox.getChildren().setAll(fxml);

现在,我没有将三个场景全部添加在一起,而是在触发我的动作触发时在两个vBox上使用了.setVisible(true / false)。

我不确定原因是什么原因,但是至少现在已经解决了。猜猜这与我调用新场景的方式有关,因为它们都使用相同的控制器,这在我的脑海中意味着它将尝试从控制器内部调用控制器,这有点递归?

如果任何人都可以澄清我的理论是否正确,我很乐于学习。

谢谢。

0 个答案:

没有答案