简体   繁体   中英

FXML setText null pointer exception

I'm fairly new to programming and I keep getting a null pointer exception and need help. I'm trying to send a variable from another class to set the text of a label. This other class is initiated when the used clicks the Enter key by the DB.DBcomm(Barcode), as seen below:

public void keyEnter(javafx.scene.input.KeyEvent event) {
    if (event.getCode() == KeyCode.ENTER) {
        Barcode = textField.getText();
        textField.setText("");
        System.out.println("Enter Key Was Pressed");

        CommController DB = new CommController();
        DB.DBcomm(Barcode);
    }
}   

At the end of the code for the other class, there is another call method as seen below:

keyListen call = new keyListen();
call.setlabelText(labeltext);

This then initiates a method in the first class as seen below:

@FXML
public void setlabelText(String text) {
    // set text from another class
    System.out.println("text received: " + text);
    label.setText(text);
}     

The console successfully prints the line

"text received: " + text

however, when it gets to

label.setText(text);

I receive the null pointer exception.

It appears you never create your label object. Trying to update an attribute of an object that does not exist will throw a NPE.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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