NullPointerException (JavaFX Label.setText())


I got problem with my javaFx application. Everytime I call the .setText() method of my Label, I get a NullPointerException. Here is my code: This is a snnippet of my Controller:

public class HomenizerController implements Initializable{
//ArrayList zum abspeichern der Termine in Listen Form
private ArrayList<Date> dateList = new ArrayList();
//ArrayList zum ab speichern der Aufgaben in Listen Form
private ArrayList<ToDo> toDoList = new ArrayList();
private Properties prop = null;

@FXML private Label username;

private void setWelcomeTab(){

private void loadProperties(String path){
    FileInputStream fis = null;
    try {
        prop = new Properties();
        fis = new FileInputStream("src/homenizer/profiles/"+path+"/");
    } catch (FileNotFoundException ex) {
        Logger.getLogger(HomenizerController.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(HomenizerController.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        try {
            if(fis != null)
        } catch (IOException ex) {
            Logger.getLogger(HomenizerController.class.getName()).log(Level.SEVERE, null, ex);


public void startHomenizer(Stage stage, String username) throws IOException{
        Parent root = FXMLLoader.load(getClass().getResource("/homenizer/view/HomenizerView.fxml"));
        Scene scene = new Scene(root,1100,650);


   public void initialize(URL url, ResourceBundle rb) {
   // TODO

And here is my .fxml:

    <TabPane fx:id="tabPan">
        <Tab text="Willkommen" closable="true" > 
                <TitledPane text="Allgemeines" expanded="true">
                        <Label text="Benutzername:" GridPane.columnIndex="0" GridPane.rowIndex="0" />
                        <Label fx:id="username" GridPane.columnIndex="1" GridPane.rowIndex="0" />
                        <Label text="Termine gesamt:" GridPane.columnIndex="0" GridPane.rowIndex="1" />
                        <Label fx:id="dates" GridPane.columnIndex="1" GridPane.rowIndex="1" /> 
                        <Label text="Aufgaben gesamt:" GridPane.columnIndex="0" GridPane.rowIndex="2" /> 
                        <Label fx:id="toDos" GridPane.columnIndex="1" GridPane.rowIndex="2" />
                <TitledPane text="Aktuell" expanded="true">
                    <GridPane fx:id="actualPane"> 

Everytime I want to set the Text of username I got this NULLPointerException:

Caused by: java.lang.NullPointerException
at homenizer.controller.HomenizerController.setWelcomeTab(
at homenizer.controller.HomenizerController.startHomenizer(
at homenizer.controller.LoginController.onStartRequest(
... 54 more

Exception in thread "JavaFX Application Thread" Deleting directory C:\Users\chris_000\Documents\NetBeansProjects\Homenizer\dist\run1609603179

So, does anybody know the problem I got?I can't find a solution :( Thanks for every help


3/3/2014 11:41:54 AM


You should set your controller into an FXMLLoader. For example, use code similar to that below inside your Homenizer start method.

FXMLLoader loader = new FXMLLoader(
Parent root = (Parent) loader.load();


why must I set the Controller inside my Controller class? I set the controller in my fxml file this didn't work

Because your startHomenizer method is not a static method, you must have already created an instance of your Homenizer class, which is also a Controller, because it has @FXML annotated members.

If you just ask the FXMLLoader to load without first setting your current instance of your Homenizer into the FXMLLoader, then the loader will create a new Hominizer instance, so you would end up with two instances, one created by the loader with the @FXML members initialized and one created by you without the @FXML members initialized. This is confusing and probably not what you want.

What you probably want is a single instance of the Homenizer class. To do that, you need to set the instance you created into the FXMLLoader and the loader will make use of your existing class instead of creating a new one (as long as there is no fx:controller defined in the loaded FXML).

There is some more discussion in the answer to: Passing Parameters JavaFX FXML.

5/23/2017 11:53:10 AM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow