java - 应用程序启动方法 java.lang.reflect.InitationTargetException 中出现异常 [NO FXML]

标签 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

最佳答案

StackTrace 指向实际问题:

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

每个节点只能添加到场景图中一次。在您的情况下,您调用 center.add(txPhoneNumber...) 两次。

当第二次尝试添加它时,编译器将失败并给出您所看到的 IllegalArgumentException

删除第二次添加 txPhoneNumber 的尝试,此错误就会消失。

关于java - 应用程序启动方法 java.lang.reflect.InitationTargetException 中出现异常 [NO FXML],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57258092/

相关文章:

php - try{...}catch 在 PHP/Symfony2/PHPImap 中不会引发异常

java - 为什么 EJB 在与 JavaFX 一起使用时会出现 nullpointerException?

java - 使用 Selenium 单击 div 标签内具有属性角色的按钮

c# - 间歇性 LifetimeScope 错误 : SelfRegistrationCannotBeActivated

java - 如何在 Intellij IDEA 的子类上下文中为重写自动完成显示方法建议?

javascript - Python 返回上一页并记住元素

java - 如何根据输入突出显示 JavaFX TableView 中的特定字符

java - 线程 "main"java.lang.ClassNotFoundException : sample. Main 中的异常 - 为什么?

支持实际结果和静态方法的 Java Mock 框架

java - 通过 JNDI 引用设置 Hibernate 的默认架构