JavaFX java.lang.reflect.InvocationTargetException

JavaFX java.lang.reflect.InvocationTargetException

我在尝试启动我的新 JavaFX 代码时遇到 java.lang.reflect.InvocationTargetException 异常。 知道为什么吗?

主要内容如下:

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);

    }   
}

这里是 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);
    }

这是我得到的完整异常:

    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$launchApplication5(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.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$launchApplication12(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait5(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null3(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater4(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$null8(WinApplication.java:191)
    ... 1 more
Exception running application App

尝试 google 它并在 Whosebug 中搜索问题,但解决方案对我的问题不起作用。

感谢您的帮助!

看起来你还没有初始化 last_name_Textphone_number_Text 所以你得到一个 NullPointerException 这最终是其他异常的原因。

好吧,我处理了 - VBox 无法添加 "null" 对象。

尝试更改

private Pane setFirst()

 private GridPane setFirst()

您正在 return 创建 GridPane,但您的 return 类型是 Pane