0

when i want to setText for TextField (miniFilePath) it throws null exception i don't know what is wrong with my code?

public class SettingsController {

@FXML
private TextField miniFilePath;

@FXML
private Button settingExitBtn;

public static String miniFilterPath = new String() ;
public static String reportDirectoryPath = new String();
Stage settings = new Stage();



public void display( ){

    try {
        FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("Settings.fxml"));

        Parent root = (Parent) fxmlLoader.load();
        //settings = new Stage();
        Stage settings = new Stage();
        settings.setScene(new Scene(root));
        settings.initModality(Modality.APPLICATION_MODAL);
        settings.setResizable(false);
        settings.setTitle("settings");

        if (!miniFilterPath.isEmpty())
            miniFilePath.setText(miniFilterPath);

        settings.show();

    } catch(Exception e) {
        e.printStackTrace();
    }

}

this class works as my second window, when i click on one button in another window one instance of this class is called and the window is made. i want that after user set text for a textfield every time user try to open this window the text field is set to the string that is set before.

ATA
  • 37
  • 1
  • 7

1 Answers1

1

Override the initialize() method before your display() method

@FXML
public void initialize() {
    refresh();
}

public void refresh() {
   if (!(miniFilterPath == null | miniFilterPath.trim().equals("")))
       miniFilePath.setText(miniFilterPath);
}

Also I assumed that you have a text field in your fxml file with the following:

 fx:id="miniFilePath"

If you don't have that, init the text field in your display() method with the following:

miniFilePath = new TextField();
gokcand
  • 6,694
  • 2
  • 22
  • 38
  • He already checks for the miniFilterPath's value(null or not). After that he tries to call setText on miniFilePath which is probably not defined in a separate .fxml file or not instantiated in his .java file. Any thoughts would be nice though, I may miss something. @GhostCat – gokcand Dec 27 '17 at 10:57
  • thank you very much... the first one works for me... – ATA Dec 27 '17 at 11:22