应用程序启动方法java.lang.reflect.InvocationTargetException [NO FXML]

时间:2019-07-29 16:41:31

标签: java exception javafx

我有一个小型javafx应用程序,并且有两个错误。如您所见,我不使用fxml。 我已经尝试清理和构建项目,然后再次运行它,但是仍然无法正常工作。我发现的所有解决方案都与fxml有关,但是我没有使用它。但是,我找不到我错了。有人可以帮我吗?

这是我的代码:

Label name = new Label("Name: ");
TextField txName = new TextField();
Label lastname = new Label("LastName  ");
TextField txLastName = new TextField();
Label age = new Label("Age: ");
TextField txAge = new TextField();
Label phoneNumber = new Label("Phone number: ");
TextField txPhoneNumber = new TextField();
Label numberCarLicence = new Label("Car licence number: ");
TextField txNumberCarLicence = new TextField();

  Label choseCar = new Label("Chose car: ");
  TextField txChoseCar = new TextField();
  Label pickLocation = new Label("Pick location: ");
  TextField txPickLocation = new TextField();
  Label backLocation = new Label("Back location: ");
  TextField txBackLocation = new TextField();
  Label pickDays = new Label("Pick days: ");
  TextField txPickDays = new TextField();

GridPane center = new GridPane();
Button btnRent = new Button("Rent");
TableView tableView = new TableView();

@Override
public void start(Stage primaryStage) {

    UnaryOperator<TextFormatter.Change> phoneNumberJustNumbers = change -> {
        String text = change.getText();
        if (text.matches("[0-9]*")) {
            return change;
        }
        return null;
    };
    TextFormatter<String> textFormatter = new TextFormatter<>(phoneNumberJustNumbers);
    txPhoneNumber.setTextFormatter(textFormatter);

    UnaryOperator<TextFormatter.Change> numberCarLicenceJustNumbers = change -> {
        String text = change.getText();
        if (text.matches("[0-9]*")) {
            return change;
        }
        return null;
    };
    TextFormatter<String> textFormatter1 = new TextFormatter<>(numberCarLicenceJustNumbers);
    txNumberCarLicence.setTextFormatter(textFormatter1);

    UnaryOperator<TextFormatter.Change> pickDaysJustNumbers = change -> {
        String text = change.getText();
        if (text.matches("[0-9]*")) {
            return change;
        }
        return null;
    };
    TextFormatter<String> textFormatter2 = new TextFormatter<>(pickDaysJustNumbers);
    txPickDays.setTextFormatter(textFormatter2);

    UnaryOperator<TextFormatter.Change> ageJustNumbers = change -> {
        String text = change.getText();
        if (text.matches("[0-9]*")) {
            return change;
        }
        return null;
    };
    TextFormatter<String> textFormatter3 = new TextFormatter<>(ageJustNumbers);
    txAge.setTextFormatter(textFormatter3);

    Scene scene = new Scene(center, 1000, 400);

    txChoseCar.setEditable(false);
    txChoseCar.setStyle("-fx-background-color : lightgray");
    txPickLocation.setEditable(false);
    txPickLocation.setStyle("-fx-background-color : lightgray");
    txBackLocation.setEditable(false);
    txBackLocation.setStyle("-fx-background-color : lightgray");
    txPickDays.setEditable(false);
    txPickDays.setStyle("-fx-background-color : lightgray");

    primaryStage.setTitle("Rent a car");
    primaryStage.setScene(scene);
    primaryStage.show();

    center.add(name, 0, 0);
    center.add(txName, 1, 0);
    center.add(lastname, 0, 1);
    center.add(txLastName, 1, 1);
    center.add(age, 0, 2);
    center.add(txPhoneNumber, 1, 2);
    center.add(phoneNumber, 0, 3);
    center.add(txPhoneNumber, 1, 3);
    center.add(numberCarLicence, 0, 4);
    center.add(txNumberCarLicence, 1, 4);
    center.add(choseCar, 3, 0);
    center.add(txChoseCar, 4, 0);
    center.add(pickLocation, 3, 1);
    center.add(txPickLocation, 4, 1);
    center.add(backLocation, 3, 2);
    center.add(txBackLocation, 4, 2);
    center.add(pickDays, 3, 3);
    center.add(txPickDays, 4, 3);
    center.add(btnRent, 5, 6);

    center.setPadding(new Insets(10, 5, 10, 5));
    center.setAlignment(Pos.CENTER);
    center.setVgap(10);
    center.setHgap(10);

    alert.show();

}

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

}

堆栈跟踪:

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:498)
    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(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    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:917)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$154(LauncherImpl.java:182)
    at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.IllegalArgumentException: Children: duplicate children added: parent = Grid hgap=0.0, vgap=0.0, alignment=TOP_LEFT
    at javafx.scene.Parent$2.onProposedChange(Parent.java:454)
    at com.sun.javafx.collections.VetoableListDecorator.add(VetoableListDecorator.java:206)
    at javafx.scene.layout.GridPane.add(GridPane.java:965)
    at cs102projekat.CS102Projekat.start(CS102Projekat.java:125)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$161(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$174(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(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$147(WinApplication.java:177)
    ... 1 more

1 个答案:

答案 0 :(得分:2)

StackTrace指向实际问题:

Caused by: java.lang.IllegalArgumentException: Children: duplicate children added: parent = Grid hgap=0.0, vgap=0.0, alignment=TOP_LEFT

每个Node只能添加到Scene图一次。就您而言,您两次致电center.add(txPhoneNumber...)

第二次尝试添加它时,编译器将失败并提供您看到的IllegalArgumentException

删除第二次添加txPhoneNumber的尝试,该错误将消失。