JavaFX java.lang.reflect.InvocationTargetException

时间:2017-09-18 08:15:55

标签: java exception javafx 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类:

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$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

尝试谷歌搜索并在StackOverFlow中搜索问题,但解决方案并不能解决我的问题。

感谢您的帮助!

3 个答案:

答案 0 :(得分:0)

看起来您尚未初始化last_name_Textphone_number_Text,因此您获得NullPointerException,这最终是其他例外的原因。

答案 1 :(得分:0)

我处理好了 - VBox无法添加“null”对象。

答案 2 :(得分:0)

尝试更改

private Pane setFirst()

 private GridPane setFirst()

您将返回GridPane,但您的返回类型为Pane