Maverick283
Maverick283

Reputation: 1402

InvocationTargetException when trying to assign items to choicebox using javafx

The simple attempt to add choices to my choice box results in a InvocationTargetException. I don't really understand the reason of why this exception is thrown, so a explanation along with a soloution would be great! Here is my code in the FXMLDocumentController Class:

public class FXMLDocumentController implements Initializable {

    @FXML
    private ChoiceBox<?> pilot;

    public FXMLDocumentController(){  

         setMembersList();
    }


    private void setMembersList(){
        List<String> list = new ArrayList<String>();
        list.add("Item A");
        list.add("Item B");
        list.add("Item C");
        ObservableList obList = FXCollections.observableList(list);
        pilot.setItems(obList);
    }
}

Here is what I get...:

Exception in Application start method

java.lang.reflect.InvocationTargetException

    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:367)
    at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:305)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:894)
    at com.sun.javafx.application.LauncherImpl.access$000(LauncherImpl.java:56)
    at com.sun.javafx.application.LauncherImpl$1.run(LauncherImpl.java:158)
    at java.lang.Thread.run(Thread.java:745)

Using trial and error the exception is definitely thrown in the line pilot.setItems(obList); as it starts up without any exceptions when I get rid of this line.

Upvotes: 0

Views: 1137

Answers (2)

Chris Jaga
Chris Jaga

Reputation: 388

Remove your ChoiceBox object (basicly every object which is defined in your FXML file) initialization from constructor and place it in (best) initialize method.

Upvotes: 1

James_D
James_D

Reputation: 209724

Your FXML-injected ChoiceBox will not be initialized at the time the constructor is called, so you will get a NullPointerException (pilot is null).

Instead, invoke your code from the initialize() method. I would also properly type your ChoiceBox and the ObservableList:

public class FXMLDocumentController {

    @FXML
    private ChoiceBox<String> pilot;

    public void initialize(){  

         setMembersList();
    }


    private void setMembersList(){
        List<String> list = new ArrayList<String>();
        list.add("Item A");
        list.add("Item B");
        list.add("Item C");
        ObservableList<String> obList = FXCollections.observableList(list);
        pilot.setItems(obList);
    }
}

Upvotes: 1

Related Questions