简体   繁体   English

运行Java FXML程序时出错

[英]Error in running Java FXML program

I'm getting an error on netbeans when trying to run my java fxml application. 尝试运行Java fxml应用程序时,netbeans出现错误。

Here is the error I am getting: 这是我得到的错误:

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 引起原因:apnc7dsimulator.UIController.handleStart(UIController.java:4‌2)位于apnc7dsimulator.UIController.handleNew(UIController.java:37)位于apnc7dsimulator.UIController.initialize(UIController.java:31‌)在javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548)...还有17个

It appears the error is originating from my controller. 看来错误是由我的控制器引起的。 Here's my code: 这是我的代码:

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);

}}

The code the controller references is this: 控制器引用的代码是这样的:

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);
}}

I'm sure it's a simple mistake I'm making or some sort of convention I'm not following correctly but this has had me stumped for a while now. 我敢肯定,这是我犯的一个简单错误,或者是某种我没有正确遵循的约定,但这已经让我难过了一段时间。 Any help would be great. 任何帮助都会很棒。

Assuming you've posted all of your code, it appears that the login member variable is never initialized. 假设您已发布所有代码,则似乎login成员变量从未初始化。 By default it is initialized to null . 默认情况下,它初始化为null The result is that when this line of code runs: 结果是当此行代码运行时:

login.loginSys(screen);

Since login is null, the code is throwing a NullPointerException . 由于login为null,因此代码将抛出NullPointerException

Somewhere in your code, before handleStart() is called, possibly in the UIController constructor, you need to initialize login , something like: 在代码中的某个位置,在handleStart()之前handleStart()可能在UIController构造函数中),您需要初始化login ,例如:

login = new Login();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM