0

I have the problem that when I call my method loginerroroutput in the Java controller from another class in another package. I cannot change the label (fx:id="loginerrormessage") with setText. I get a NullPointerException as error. The method is called, but the label is not found. When I call this method (loginerroroutput) through a method of my controller, it works.

Controller Code

package gui;


import javafx.fxml.FXML;
import javafx.scene.control.Label;

import java.net.URL;
import java.util.ResourceBundle;

public class LoginController {

    private ResourceBundle resources;

    private URL location;

    @FXML
    public Label Loginerrormessage;

    //  Aufruf vom Eventbus für alle Fehler für das Login-> Fehlerausgabe als String wird angezeigt
    public void loginerroroutput(String string){   
        System.out.println(string);                  //Console output string
        Loginerrormessage.setText(string);           // Console output null

    }
    public void initialize() {
    }
}

FXML Code

I'm pretty sure it's the initialize() method because I think that method is responsible for the post-processing processes.

Thank you for your tips and suggestions.

fabian
  • 80,457
  • 12
  • 86
  • 114
  • Where do you call this method from? I don't see a main, or an Application. – matt May 16 '18 at 12:37
  • As @matt, I suspect that you are not getting your `LoginController` instance with `FXMLLoader.getController()` on the calling site. – kagmole May 16 '18 at 12:46
  • Don't post code as images, but as text. (In this case the xml markup may not have been visible, but if you use the correct markup for code blocks (4x space at the start of every line and 1 empty line at the start) it should work.) – fabian May 16 '18 at 13:33
  • The answers to this question demonstrate how to do this properly btw: https://stackoverflow.com/questions/14187963/passing-parameters-javafx-fxml – fabian May 16 '18 at 13:42

1 Answers1

0

You should save an anywhere LoginController object to call exactly this object.

For example:

FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getClassLoader().getResource("loginController.fxml"));
LoginController loginController = loader.getController();
AnyWhereStorage.setLoginController(loginController);  

And then in any place:

AnyWhereStorage.getLoginController.loginerroroutput("Error");
Joao
  • 374
  • 3
  • 16