gpt4 book ai didi

java - 运行Java FXML程序时出错

转载 作者:行者123 更新时间:2023-12-02 11:00:57 24 4
gpt4 key购买 nike

尝试运行Java fxml应用程序时,netbeans出现错误。

这是我得到的错误:

Caused by: java.lang.NullPointerException at apnc7dsimulator.UIController.handleStart(UIController.java:4‌​2) at apnc7dsimulator.UIController.handleNew(UIController.java:37) at apnc7dsimulator.UIController.initialize(UIController.java:31‌​) at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548) ... 17 more



看来错误是由我的 Controller 引起的。这是我的代码:
public class UIController implements Initializable {

@FXML
private TextArea screen;

@FXML
private TextField commandLine;

private Login login;

@Override
public void initialize(URL location, ResourceBundle resources) {
handleNew();
}

public void handleNew(){
screen.clear();
commandLine.clear();
handleStart();

}

public void handleStart(){
login.loginSys(screen);

}}

Controller 引用的代码是这样的:
public class Login implements PrintToScreen{

@Override
public void print(String text, TextArea screen) {
screen.appendText(text);
}

@Override
public String read(TextArea screen) {
Scanner scanner = new Scanner(System.in);
String readString = scanner.nextLine();
while(readString != null){

print(readString,screen);

if(scanner.hasNextLine()){
readString = scanner.nextLine();
}
else{
readString = null;
}
}
return readString;
}

public void loginSys (TextArea screen)
{
String user = "...";
String pass = "...";

print("Please Enter Username:",screen);
user = read(screen);

print("Please Enter Password:",screen);
pass = read(screen);

print("Check Credentials... Please Wait.",screen);
}}

我敢肯定这是我犯的一个简单错误,或者是某种我没有正确遵循的约定,但这已经让我难过了一段时间。
任何帮助都会很棒。

最佳答案

假设您已发布所有代码,则login成员变量似乎从未初始化。默认情况下,它被初始化为null。结果是当此行代码运行时:

login.loginSys(screen);

由于 login为null,因此代码将抛出 NullPointerException

在代码中的某个位置,在调用 handleStart()之前,可能在 UIController构造函数中,您需要初始化 login,类似于:
login = new Login();

关于java - 运行Java FXML程序时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40772682/

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