1

I get an InvocationTargetException when I try to call a method from the controller (JavaFX), the code goes as follow:

    public void start(Stage stage) throws Exception {

    URL location = getClass().getResource("startScreen.fxml");
    FXMLLoader loader = new FXMLLoader();

    loader.setLocation(location);
    loader.setBuilderFactory(new JavaFXBuilderFactory());

    //Getting hte controller
    StartScreenController s = (StartScreenController)loader.getController();

    //Where error occurs
    s.setParent(this);

    Parent root = (Parent) loader.load(location.openStream());

    Scene scene = new Scene(root);
    stage.setScene(scene);
    stage.show();
}

It works fine when I remove the "setParent" line.

This seem to have worked for others (as far as from what I have seen when trying to solve this myself).

Any help would be appreciated, thanks.

The error I get (s.setParent is line 38)

    Exception in Application start method
java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at com.javafx.main.Main.launchApp(Main.java:642)
    at com.javafx.main.Main.main(Main.java:805)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:403)
    at com.sun.javafx.application.LauncherImpl.access$000(LauncherImpl.java:47)
    at com.sun.javafx.application.LauncherImpl$1.run(LauncherImpl.java:115)
    at java.lang.Thread.run(Thread.java:722)
Caused by: java.lang.NullPointerException
    at johnsoft.dndcommunication.DndCommunication.start(DndCommunication.java:38)
    at com.sun.javafx.application.LauncherImpl$5.run(LauncherImpl.java:319)
    at com.sun.javafx.application.PlatformImpl$5.run(PlatformImpl.java:206)
    at com.sun.javafx.application.PlatformImpl$4.run(PlatformImpl.java:173)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.access$100(WinApplication.java:29)
    at com.sun.glass.ui.win.WinApplication$3$1.run(WinApplication.java:73)
    ... 1 more
Java Result: 1
4
  • it seems you're trying to call Main class stuff inside controller , see this link stackoverflow.com/questions/14059954/… Commented Feb 9, 2013 at 22:22
  • Has the InvocationTargetException a root cause? If yes, which one? Commented Feb 10, 2013 at 0:22
  • I will post the whole error message, though it says in the comments where the error occurs. Commented Feb 10, 2013 at 11:18
  • @JohnMikaelGundersen: can you rearrange your answer? how did you get working? I still can't get out of this problem? Commented Mar 10, 2014 at 5:30

1 Answer 1

2

I guess,
you are trying to access to the controller class before loading the FXML file.

StartScreenController s = (StartScreenController)loader.getController();

// so StartScreenController s is null here thus NPE
s.setParent(this);

// here loading is happening after getting the controller class, which is wrong. 
// Get the controller after loading is completed.

Parent root = (Parent) loader.load(location.openStream());
Sign up to request clarification or add additional context in comments.

1 Comment

@Uluk Biy: can you help me with this? stackoverflow.com/q/22243243/2722799

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.