英文:
Receiving an error in Java and don't understand the problem: Exception in Application init method java.lang.reflect.InvocationTargetException
问题
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;
import website.WebsiteRootPane;
public class ApplicationLoader extends Application {
private WebsiteRootPane root;
@Override
public void init() {
root = new WebsiteRootPane();
}
@Override
public void start(Stage stage) throws Exception {
stage.setMinWidth(1000);
stage.setMinHeight(750);
stage.setTitle("Planet Generator");
stage.setScene(new Scene(root));
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
package website;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
public class CreateAccountPane extends GridPane {
private ComboBox<String> cboTitle;
private TextField txtSurname, txtFirstName, txtEmail;
private Button btnCreate;
private PasswordField pwrd;
public CreateAccountPane() {
Label lblTitle = new Label("Title");
Label lblFirstName = new Label("First name");
Label lblSurname = new Label("Surname");
Label lblEmail = new Label("Email");
Label lblPassword = new Label("Password");
ObservableList<String> titles = FXCollections.observableArrayList("Mr", "Mrs", "Miss", "Ms");
cboTitle = new ComboBox<String>(titles);
cboTitle.getSelectionModel().select(0);
txtFirstName = new TextField();
txtSurname = new TextField();
txtEmail = new TextField();
pwrd = new PasswordField();
this.add(lblTitle, 0, 0);
this.add(cboTitle, 1, 0);
this.add(lblFirstName, 0, 1);
this.add(txtFirstName, 1, 1);
this.add(lblSurname, 0, 2);
this.add(txtSurname, 1, 2);
this.add(lblEmail, 0, 3);
this.add(txtEmail, 1, 3);
this.add(lblPassword, 0, 4);
this.add(pwrd, 1, 4);
this.getChildren().add(txtFirstName);
}
}
If you need further assistance, please let me know.
英文:
I'm developing a program using JavaFX and I see no errors in my code (not underlined), yet when I run the code using a seperate file, I am getting the following error:
Here is the full stack trace:
Exception in Application init 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 init method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:912)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$1(LauncherImpl.java:182)
at java.lang.Thread.run(Unknown Source)
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 website.CreateAccountPane.<init>(CreateAccountPane.java:60)
at website.WebsiteRootPane.<init>(WebsiteRootPane.java:22)
at main.ApplicationLoader.init(ApplicationLoader.java:14)
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:841)
... 2 more
Exception running application main.ApplicationLoader
This is the code for my application launcher:
package main;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;
import website.WebsiteRootPane;
public class ApplicationLoader extends Application {
private WebsiteRootPane root;
@Override
public void init() {
root = new WebsiteRootPane();
}
@Override
public void start(Stage stage) throws Exception {
stage.setMinWidth(1000);
stage.setMinHeight(750);
stage.setTitle("Planet Generator");
stage.setScene(new Scene(root));
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
This is one of the tab panes for my website, so far it is the only one I've built.
package website;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
public class CreateAccountPane extends GridPane {
private ComboBox<String> cboTitle;
private TextField txtSurname, txtFirstName, txtEmail;
private Button btnCreate;
private PasswordField pwrd;
public CreateAccountPane() {
//create labels
Label lblTitle = new Label("Title");
Label lblFirstName = new Label("First name");
Label lblSurname = new Label("Surname");
Label lblEmail = new Label("Email");
Label lblPassword = new Label("Password");
// setup combobox
ObservableList<String> titles = FXCollections.observableArrayList("Mr", "Mrs", "Miss", "Ms");
cboTitle = new ComboBox<String>(titles);
cboTitle.getSelectionModel().select(0);
// setup text fields
txtFirstName = new TextField();
txtSurname = new TextField();
txtEmail = new TextField();
pwrd = new PasswordField();
this.add(lblTitle, 0, 0);
this.add(cboTitle, 1, 0);
this.add(lblFirstName, 0, 1);
this.add(txtFirstName, 1, 1);
this.add(lblSurname, 0, 2);
this.add(txtSurname, 1, 2);
this.add(lblEmail, 0, 3);
this.add(txtEmail, 1, 3);
this.add(lblPassword, 0, 4);
this.add(pwrd, 1, 4);
this.getChildren().add(txtFirstName);
}
}
答案1
得分: 4
问题
你将 txtFirstName
添加了两次,但是在 UI 中只能添加一个元素。
你分别使用以下方式添加它:
this.add(txtFirstName, 1, 1);
和
this.getChildren().add(txtFirstName);
你可以通过移除以下代码来解决问题:
this.getChildren().add(txtFirstName);
如何找到问题(阅读这段内容,它会帮助你:))
看看你的异常信息:
> Exception in Application init method java.lang.reflect.InvocationTargetException
这表示问题发生在执行 init
方法的代码时。
InvokedTargetException
表示使用反射调用方法(像 JavaFX 中的 init
方法一样)时抛出了一个异常。这可以是任何异常。堆栈跟踪中的 Caused by
部分显示了真正的异常。让我们来看一下:
> 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 website.CreateAccountPane.<init>(CreateAccountPane.java:60)
异常信息(Children: duplicate children added: parent = Grid hgap=0.0, vgap=0.0, alignment=TOP_LEFT
)告诉你问题是已经添加了该元素。
> at website.CreateAccountPane.<init>(CreateAccountPane.java:60)
告诉你它发生在 CreateAccountPane
的第 60 行(CreateAccountPane.java:60
)。
接下来,我们仔细看看这一行代码:
this.getChildren().add(txtFirstName);
它实际上在网格面板中添加了一个元素。
所以,看起来这个元素已经是网格面板的一部分。那么,让我们看看之前你添加了什么:
this.add(txtFirstName, 1, 1);
你重复添加了相同的元素。这就是问题所在。
结论
正如你所看到的,阅读和解释堆栈跟踪是编程非常重要的一部分。如果继续进行,你会看到越来越多类似的堆栈跟踪。
不要害怕那些巨大的红色文本。将它们分开并尝试找到问题。
这可能会耗费时间且困难,尤其是在开始阶段,但随着时间的推移,你会变得更好!
发现、理解和修复错误是编程非常重要的一部分(也许是最重要的一部分)。
如果你能够
- 阅读和解释异常
并且
- 进行调试。如果你不知道什么是调试,请查找相关信息,学习并尝试一下。这几乎肯定会帮助你。
英文:
The problem
You are adding txtFirstName
twice but you can only add an element to the UI once.
You add it with
this.add(txtFirstName, 1, 1);
and
this.getChildren().add(txtFirstName);
You should be able to fix it by removing
this.getChildren().add(txtFirstName);
How to find that (read that, it will help you :))
Look at your exception:
> Exception in Application init method java.lang.reflect.InvocationTargetException
This says that the problem occurs while executing the code of init
.
InvokedTargetException
means that a method invoked using reflection(what is the case with init as JavaFX uses reflection for this) threw an exception. This could be any exception. The Caused by
section in the stack trace shows the real exception. Let's look at it:
> 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 website.CreateAccountPane.<init>(CreateAccountPane.java:60)
The exception message (Children: duplicate children added: parent = Grid hgap=0.0, vgap=0.0, alignment=TOP_LEFT
) tells you that the problem is that you have already added that element.
> at website.CreateAccountPane.<init>(CreateAccountPane.java:60)
tells you that it occured in line 60 of CreateAccountPane
(CreateAccountPane.java:60
).
Next, we look closer at this line:
this.getChildren().add(txtFirstName);
It actually adds an element to the grid pane.
So, it seems that this element is already a part of the grid pane. So, let's see what you've added to it previously:
this.add(txtFirstName, 1, 1);
You added the same element two times. That is the problem.
Conclusion
As you see, reading and interpreting stack traces is a very, very important part of programming. If you go on, you will see more and more stack traces like this.
Don't fear those huge red texts. Split them apart and try to find the problem.
This may be time consuming and difficult, especially at the beginning but with time, you will get better!
Finding, understanding and fixing bugs is a very important part of programming(maybe the most important part).
This part will be much easier if you can
- read and interpret exceptions
and
- debug. If you don't know what debugging is, look it up, learn it and try it out. It will almost surely help you.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论