0

I'm trying to do a java fxml exercise but my program keeps crashing at Pane and viewController. I checked with the debugger and it seems it returns null values.

I've checked for uninitialized objects, I wrote the .fxml file path in all kinds of ways and I've tried some of these ideas: JAVAFX - Load fxml with anchorpane inside main fxml; Error in Loading FXML file into Anchor Pane in JavaFX and others but I still haven't figured it out.

If you have any ideas please share them :)

public void start(Stage stg) throws Exception 
{
    stg.setTitle("First FXML");
    FXMLLoader loader = new FXMLLoader();
    loader.setLocation(getClass().getResource("/PbFXML/src/Controller/first.fxml"));
    Pane pane = (Pane)loader.load(); --here

    viewController ctr = loader.getController(); --here
    ctr.setService(getService());

    Scene sc = new Scene(pane);
    stg.setScene(sc);
    stg.show();
}
Community
  • 1
  • 1
shanto
  • 11
  • 4
  • It happens cause probably it can't load the path cause the location is not correct . Have a look http://stackoverflow.com/questions/40921616/java-lang-nullpointerexception-location-is-required/40921681#40921681 and copy the code from the `Main.java` to see if the path is correct . Then please add a 1) stack trace :) 2)Controller the main parts 3)the fxml for analysis and the stackoverflow team will find the root of problem – GOXR3PLUS Dec 04 '16 at 22:36
  • Thanks! I managed to solve the problem. Apparently the .fxml file is supposed to be in a 'resources' folder. I work in Eclipse and maybe that's why (I understand that in Intellij this is not necessary). – shanto Dec 05 '16 at 12:17
  • You can place the `.fxml` wherever you want . Using a folder named resources is actually a good way ;) – GOXR3PLUS Dec 05 '16 at 12:19

0 Answers0