user11853373
user11853373

Reputation:

Exception in Application start method java.lang.reflect.InvocationTargetException [NO FXML]

I have small javafx app and i have two errors. As you can see I dont use fxml. I already tried to clean and build project and then run it again, but still doesnt work. All the solutions I found are related to fxml, but I did not use it. However, I can not find where I'm wrong. Can somebody help me?

Here is my code:

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

}

Stack Trace:

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

Upvotes: 0

Views: 1388

Answers (1)

Zephyr
Zephyr

Reputation: 10253

The StackTrace points to the actual issue:

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

Each Node may only be added to a Scene graph once. In your case, you call center.add(txPhoneNumber...) twice.

When trying to add it the second time, the compiler will fail and give the IllegalArgumentException you're seeing.

Remove the second attempt to add txPhoneNumber and this error will go away.

Upvotes: 2

Related Questions