JavaFX java.lang.reflect.InvocationTargetException

5.1k Views Asked by At

I got a java.lang.reflect.InvocationTargetException Exception when trying to launch my new JavaFX code. Any idea why?

Here's the main:

public class App extends Application{
    ContactsManager cm;
    Controller controller;

    public static void main(String[] args) {
        App.launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        cm = new ContactsManager("contacts.dat");
        controller = new Controller(cm);
        IFrame cmf = new ContactsManagerFrame();
        controller.addView(cmf);
        IFrame cmFX = new ContactsManagerFX(primaryStage);
        controller.addView(cmFX);

    }   
}

And here's the ContactsManagerFX class:

public class ContactsManagerFX implements IFrame {
    Stage primaryStage;
    private TextField first_name_Text, last_name_Text, phone_number_Text, file_path_Text;
    private Button create_Button, update_Button, prev_Button, next_Button, first_contact_Button, last_contact_Button, edit_Button, export_Button, load_file_Button, sort_Button, show_Button;
    private Label first_name_Label, last_name_Label, phone_number_Label;
    private ComboBox<String> comBox_panel_3,comBox_Sort,comBox_Fields,comBox_Order_1,comBox_Order_2;
    private String[] comBox_array_panel_3 = { "txt", "obj.dat", "byte.dat" };
    private String[] comBox_array_Sort = { "Sort - Field", "Sort - Count", "Reverse" };
    private String[] comBox_array_Fields = { "First_Name_Field", "Last_Name_Field", "Phone_Number_Field" };
    private String[] comBox_array_Order = { "Asc", "Desc" };
    private ArrayList<ActionListener> listeners = new ArrayList<ActionListener>();

    public ContactsManagerFX(Stage stage) {
        primaryStage = stage;
        VBox mainLay = new VBox(20);
        mainLay.getChildren().add(setFirst());

        Pane mainPane = new Pane();
        mainPane.getChildren().add(mainLay);
        Scene mainScene = new Scene(mainPane);
        primaryStage.setScene(mainScene);
    }

    private Pane setFirst() {
        GridPane firstPane = new GridPane();
        firstPane.setAlignment(Pos.CENTER);
        firstPane.setHgap(6);
        firstPane.setVgap(6);
        firstPane.add(new Label("First name: "),0,0);
        firstPane.add(first_name_Text = new TextField(), 0, 1);
        firstPane.add(new Label("Last name: "), 0, 1);
        firstPane.add(last_name_Text = new TextField(), 1, 1);
        firstPane.add(new Label("Phone number: "), 0, 2);
        firstPane.add(phone_number_Text = new TextField(), 1, 2);
        firstPane.add(create_Button = new Button("Create"), 0, 3);
        firstPane.add(update_Button = new Button("Update"), 1, 3);

        return firstPane;
    }

    @Override
    public void init() {
        primaryStage.show();
        primaryStage.setAlwaysOnTop(true);
    }

And Here's the full Exception I got:

    Exception in Application start method
java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
    at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at sun.launcher.LauncherHelper$FXHelper.main(Unknown Source)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$155(LauncherImpl.java:182)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NullPointerException: Children: child node is null: parent = VBox@5a229a41
    at javafx.scene.Parent$2.onProposedChange(Parent.java:435)
    at com.sun.javafx.collections.VetoableListDecorator.add(VetoableListDecorator.java:206)
    at ContactsManagerFX.<init>(ContactsManagerFX.java:39)
    at App.start(App.java:22)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
    ... 1 more
Exception running application App

Tried to google it and search the problem in StackOverFlow but the solutions didn't work for my problem.

Thanks for your help!

3

There are 3 best solutions below

2
On BEST ANSWER

Well I handled it - the VBox can't add a "null" object.

1
On

It looks like that you haven't initialized last_name_Text and phone_number_Text so you get a NullPointerException which is ultimately the reason for the other exceptions.

0
On

Try changing

private Pane setFirst()

to

 private GridPane setFirst()

You are returning a GridPane, but your return type is Pane.