If I try to get or set a text from a Label object that is present in my FXML file, a NullPointerException is thrown despite every other Label being recognized as pointing to the correct element. Some people managed to fix this problem by overriding the initialize method, but that didn't seem to work for me. The object I'm referring to is the Label "error", the code is the following:
public class InputController {
public TextField campoNome;
public PasswordField campoPsw;
public Label switchInput, error;
public Button invio, chiudi;
private static ArrayList<Utente> utenti;
private static final ObjectMapper om = new ObjectMapper();
@FXML
protected void loginInvio() throws IOException {
String nome, psw;
boolean check=false;
nome = campoNome.getText();
psw = campoPsw.getText();
try{utenti = om.readValue(new FileInputStream("src/main/resources/com/example/gestionedeiruoli/utenti.json"), new TypeReference<>(){}); check=true;}
catch(MismatchedInputException ignore){}
catch(Exception e){System.exit(-1);}
if(check){
for (Utente e : utenti) {
if (nome.equals(e.getNome()) && psw.equals(e.getPsw())) {
try {switchPrincipale(invio);}
catch (Exception e2){System.exit(-1);}
return;
}
}
}
Parent root = FXMLLoader.load(getClass().getResource("error.fxml"));
Scene scene = new Scene(root);
Stage stage = new Stage();
stage.setScene(scene);
stage.setResizable(false);
stage.setTitle("Error!");
stage.show();
System.out.println(error.getText());
}}
The issue occurs whenever the program gets to the last line, in "System.out.println(error.getText());"