gpt4 book ai didi

JavaFX getText()、setText()、toString() 给出空指针

转载 作者:太空宇宙 更新时间:2023-11-04 10:24:56 24 4
gpt4 key购买 nike

Controller.java

import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;

public class Controller {

public TextField txtField;
public PasswordField passField;
public Button btn;
public Label access;

private String USNM = "crimsoncoder";
private String PASS = "toor";

▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬问题从这里开始▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬

    private String usnm = txtField.getText();
private String pass = passField.getText();
private String acc = access.getText();

private final String ACCESS_GRANTED = "/ACCESS_GRANTED/";
private final String ACCESS_DENIED = "|/?ACCESS_DENIED?/|";

public void onClick() {
if (usnm.equals(USNM)) {
if (pass.equals(PASS)) {
acc = ACCESS_GRANTED;
} else {
acc = ACCESS_DENIED;
}
} else {
acc = ACCESS_DENIED;
}
}
}

在 Controller.java 中声明 usnmpassacc 变量后调用方法 getText()setText()toString() 会返回 NullPointerException。知道为什么会发生这种情况吗?

<小时/>

新代码:

public class Controller {

@FXML
private TextField txtField;
@FXML
private PasswordField passField;
@FXML
private Button btn;
@FXML
private Label access;

private String USNM = "crimsoncoder";
private String PASS = "toor";

private static final String ACCESS_GRANTED = "/ACCESS_GRANTED/";
private static final String ACCESS_DENIED = "|/?ACCESS_DENIED?/|";

public void onClick() {
String usnm = txtField.getText();
String pass = passField.getText();

if (usnm.equals(USNM)) {
if (pass.equals(PASS)) {
access.setText(ACCESS_GRANTED);
} else {
access.setText(ACCESS_DENIED);
}
} else {
access.setText(ACCESS_DENIED);
}
}
}

最佳答案

您的设计实际上存在很多问题。首先,您尝试在 txtFieldpassField 控件实际存在于场景中之前访问这些控件的值。

使用 JavaFX 和 FXML,在场景初始化之前,这些控件不会构建(或注入(inject))并可供 Controller 使用。

控件引用也应该是私有(private)并用@FXML关键字进行注释,如下所示:

@FXML
private TextField txtField;

这实际上告诉 Java TextField 已在关联的 FXML 文档中定义。

并且您需要在单击 LOGIN 按钮之后获取用户名和密码字段的值,因此请更新您的 onClick() 方法以检索当时的值:

public void onClick() {

String usnm = txtField.getText();
String pass = passField.getText();

String acc;

if (usnm.equals(USNM)) {
if (pass.equals(PASS)) {
acc = ACCESS_GRANTED;
} else {
acc = ACCESS_DENIED;
}
} else {
acc = ACCESS_DENIED;
}

System.out.println(acc);
}

因此,对于稍微好一点的 Controller.java,您可以使用如下内容:

public class Controller {

@FXML
public TextField txtField;
@FXML
public PasswordField passField;
@FXML
public Button btn;
@FXML
public Label access;

private String USNM = "crimsoncoder";
private String PASS = "toor";

private static final String ACCESS_GRANTED = "/ACCESS_GRANTED/";
private static final String ACCESS_DENIED = "|/?ACCESS_DENIED?/|";

public void onClick() {

String usnm = txtField.getText();
String pass = passField.getText();

String acc;

if (usnm.equals(USNM)) {
if (pass.equals(PASS)) {
acc = ACCESS_GRANTED;
} else {
acc = ACCESS_DENIED;
}
} else {
acc = ACCESS_DENIED;
}

System.out.println(acc);
}

}

关于JavaFX getText()、setText()、toString() 给出空指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50660863/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com