Issue
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";
▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬The Issue Starts Here▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬
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;
}
}
}
Calling the methods getText()
,setText()
and toString()
after declaring the usnm
, pass
and acc
variables within Controller.java returns NullPointerException
. Any idea why this is happening?
New Code:
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);
}
}
}
Solution
There are actually quite a few issues with your design here. First of all, you are trying to access the values of the txtField
and passField
controls before those controls actually exist in the scene.
With JavaFX and FXML, those controls are not built (or injected) and available to the controller until the scene has been initialized.
The control references should also be private
and annotated with the @FXML
keyword, like so:
@FXML
private TextField txtField;
This essentially tells Java that the TextField
has already been defined in the associated FXML document.
And you need to get the values of the username and password fields after the LOGIN
button has been clicked, so update your onClick()
method to retrieve the values at that time:
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);
}
So, for a slightly better Controller.java
, you could use something like this:
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);
}
}
Answered By - Zephyr