Ertan Hasani
Ertan Hasani

Reputation: 817

JavaFx - TextField.setText() throwing nullpointerException

im trying to add some text to a textfield, but when i click the button it shows nullpointerexception , why is this happening?

MainWindowController.java

@FXML
public TextField konsumatoriPunetField = new TextField();

@FXML
private void initialize()
{
    FXMLLoader loader5 = new FXMLLoader();
    loader5.setLocation(getClass().getResource("ZgjedhKonsumatorinFXML.fxml"));
    BorderPane border5 = new BorderPane();
    border5 = loader5.load();
    Scene scene5 = new Scene(border5);
    zgjedhkonsumatorinstage.setScene(scene5);
    zgjedhkonsumatorinstage.setTitle("Pit Stop");
    zgjedhkonsumatorinstage.initModality(Modality.WINDOW_MODAL);
    zgjedhkonsumatorinstage.initOwner(MainFXMLController.mainFXMLStage);
}

@FXML
public void zgjedhKonsumatorin()
{
    zgjedhkonsumatorinstage.showAndWait();
}

MainWindowFXML.fxml

<TextField fx:id="konsumatoriPunetField" editable="false" onMouseClicked="#zgjedhKonsumatorin" promptText="Kliko per te zgjedhur" GridPane.columnIndex="1" GridPane.rowIndex="1" />

ZgjedhKonsumatorinController.java

@FXML
public void zgjedhKonsumatorin()
{
    FXMLLoader loader = new FXMLLoader(getClass().getResource("MainWindowFXML.fxml"));
    MainWindowController c = (MainWindowController) loader.getController();
    c.konsumatoriPunetField.textProperty().setValue("ertani");
    main.zgjedhkonsumatorinstage.close();

}

ZgjedhKonsumatorinFXML.fxml

<Button mnemonicParsing="false" onAction="#zgjedhKonsumatorin" prefWidth="150.0" text="Zgjedh Konsumatorin" />

Output:

Caused by: java.lang.NullPointerException
    at main.ZgjedhKonsumatorinController.zgjedhKonsumatorin(ZgjedhKonsumatorinController.java:193)
    ... 102 more

p.s. this is the line 193 in ZgjedhKonsumatorinController (exception)

c.konsumatoriPunetField.textProperty().setValue("ertani");

Upvotes: 0

Views: 5194

Answers (1)

James_D
James_D

Reputation: 209330

The controller is created by the FXMLLoader when you load the FXML file (the controller class is specified by the FXML file, so this is the only time it could be created). So if you call loader.getController() before calling load(), the value returned will be null. Hence in your code c is null and you get a null pointer exception.

Note that it won't actually help to call loader.load() here. It would fix the null pointer exception, but of course you would load a new instance of the UI defined by the FXML file, and a new instance of the controller. Hence the text field whose text you are setting would not be the text field that is displayed, and nothing would happen.

Since you are using showAndWait() on the window you create, the easiest way to set the text is just to do it back in the MainWindowController, after the showAndWait() call completes. showAndWait() blocks execution until the window is closed, so the text field won't change until the window is closed.

First define a method in ZgjedhKonsumatorinController for retrieving the text:

public class ZgjedhKonsumatorinController {

    @FXML
    public void zgjedhKonsumatorin()
    {
        main.zgjedhkonsumatorinstage.close();
    }

    public String getText() {
        // in real life you can get text from the controls in ZgjedhKonsumatorinFXML.fxml
        return "ertani" ;
    }
}

and now back in MainWindowController you can do:

public class MainWindowController {

    @FXML
    // Note: it is ALWAYS a mistake to initialize @FXML-injected fields.
    // Just declare them and let the FXMLLoader initialize them
    // (that is the whole point of @FXML)
    private TextField konsumatoriPunetField ;

    private ZgjedhKonsumatorinController zgjedhKonsumatorinController ;

    @FXML
    private void initialize()
    {
        FXMLLoader loader5 = new FXMLLoader();
        loader5.setLocation(getClass().getResource("ZgjedhKonsumatorinFXML.fxml"));
        BorderPane border5 = new BorderPane();
        border5 = loader5.load();
        zgjedhKonsumatorinController = loader.getController();
        Scene scene5 = new Scene(border5);
        zgjedhkonsumatorinstage.setScene(scene5);
        zgjedhkonsumatorinstage.setTitle("Pit Stop");
        zgjedhkonsumatorinstage.initModality(Modality.WINDOW_MODAL);
        zgjedhkonsumatorinstage.initOwner(MainFXMLController.mainFXMLStage);
    }

    @FXML
    public void zgjedhKonsumatorin()
    {
        zgjedhkonsumatorinstage.showAndWait();
        konsumatoriPunetField.setText(zgjedhKonsumatorinController.getText());
    }

}

Upvotes: 1

Related Questions