NullPointerException (JavaFX Label.setText())


Question

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(){
    username.setText("A");
}

private void loadProperties(String path){
    FileInputStream fis = null;
    try {
        prop = new Properties();
        fis = new FileInputStream("src/homenizer/profiles/"+path+"/config.properties");
        prop.load(fis);
    } 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)
                fis.close();
        } 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);
        stage.setResizable(true);
        stage.setTitle("Homenizer");
        stage.setScene(scene);
        stage.show();
        loadProperties(username);
        setWelcomeTab();

    }

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

And here is my .fxml:

<center>
    <TabPane fx:id="tabPan">
        <Tab text="Willkommen" closable="true" > 
            <VBox>
                <TitledPane text="Allgemeines" expanded="true">
                    <GridPane>
                        <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" />
                    </GridPane>
                </TitledPane>
                <TitledPane text="Aktuell" expanded="true">
                    <GridPane fx:id="actualPane"> 
                    </GridPane> 
                </TitledPane>
            </VBox>
        </Tab>
    </TabPane>
</center>

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

Caused by: java.lang.NullPointerException
at homenizer.controller.HomenizerController.setWelcomeTab(HomenizerController.java:44)
at homenizer.controller.HomenizerController.startHomenizer(HomenizerController.java:76)
at homenizer.controller.LoginController.onStartRequest(LoginController.java:107)
... 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

-GhostfaceChilla-

1
2
3/3/2014 11:41:54 AM

Solution

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(
        getClass().getResource("/homenizer/view/HomenizerView.fxml")
);
loader.setController(this);
Parent root = (Parent) loader.load();

Explanation

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.

6
5/23/2017 11:53:10 AM

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