1

im trying to learn about JavaFX and bumped into NullPointerException for Controller. Meaning everytime i use the instance of controller, eclipse show the Exception. I have read severals similar questions but non of this solved my case.

Here is the code snippet that caused the Exception

public GuiController (Stage a) throws IOException  {
    this.priStage = a;
    FXMLLoader fxLoader = new FXMLLoader(getClass().getClassLoader().getResource("View/OverviewWindow.fxml"));  
    overviewController = fxLoader.getController();
    overviewController.getRefreshButton().setText("Oh no"); **// When i remove this line everything works fine!**
    Parent root = fxLoader.load();
    priStage.setScene(new Scene(root));
    priStage.show();
}

Here is my Controller class, basicly nothing :

public class OverviewWindow implements Initializable {
    @FXML TextField searchTextField;

    @FXML
    private Button standardButton;

    @FXML
    private Button refreshButton;

    @FXML
    private RadioButton mitArchivRadioButton;

    @FXML
    private RadioButton nurArchivButton;

    @Override
    public void initialize(URL a, ResourceBundle b) {

    }
    public OverviewWindow() {

    }

}

my stack trace

at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NullPointerException
at Controller.GuiController.<init>(GuiController.java:31)
at Controller.Controller.init(Controller.java:33)
at main.start(main.java:24)

Can someone help me ? I read about this problem for days and tried manyway to set the controller right but couldnt do it. Thanks.

  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Eli Sadoff Nov 28 '16 at 17:02
  • 1
    I pretty much have some ideas of NullPointerException, but didnt really know why this is the case. I initialized the Controller object, set it in FXMLLoader and still this happens. – Anh Tuấn Phạm Nov 28 '16 at 17:04

1 Answers1

0

first you have to load fxml after write this code..

overviewController = fxLoader.getController();
overviewController.getRefreshButton().setText("Oh no");
CTN
  • 335
  • 1
  • 18