为什么“java.lang.reflect.InvocationTargetException”显示
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21732894/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why "java.lang.reflect.InvocationTargetException" showing
提问by TomJ
I am developing a project in javafx
using NetBeans IDE
. Whenever I run my code I am getting some exceptions. There was no problems till yesterday. The exceptions are given below :
我正在开发一个javafx
使用NetBeans IDE
. 每当我运行我的代码时,我都会遇到一些异常。直到昨天都没有问题。例外情况如下:
Executing E:\Project\WelcomePage\dist\run204992192\WelcomePage.jar using platform C:\Program Files\Java\jdk1.7.0_45\jre/bin/java
Exception in Application constructor
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.javafx.main.Main.launchApp(Main.java:698)
at com.javafx.main.Main.main(Main.java:871)
Caused by: java.lang.RuntimeException: Unable to construct Application instance: class welcomepage.WelcomePage
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:393)
at com.sun.javafx.application.LauncherImpl.access class WelcomePage extends Application {
@Override
public void start(Stage stage33) {
BorderPane border = new BorderPane();
border.setTop(addVBox());
border.setLeft(addVBox1());
Scene scene = new Scene(border,700,450);
stage33.setScene(scene);
stage33.setResizable(false);
scene.getStylesheets().add
(WelcomePage.class.getResource("WelcomePage.css").toExternalForm());
stage33.show();
}
private VBox addVBox() {
VBox vbox = new VBox();
vbox.setPadding(new Insets(5, 12, 5, 20));
vbox.setSpacing(10); // Gap between nodes
//vbox.setStyle("-fx-background-color: #999999;");
Image image = new Image(getClass().getResourceAsStream("logo11.png"));
Label lb1=new Label(" C - MARK AND ATTENDANCE CALCULATOR");
lb1.setAlignment(Pos.CENTER);
lb1.setFont(Font.font("Calibri",FontWeight.BOLD,28));
lb1.setTextFill(Color.BLACK);
lb1.setGraphic(new ImageView(image));
vbox.getChildren().addAll(lb1);
return vbox;
}
private VBox addVBox1()
{
VBox vbox1=new VBox();
vbox1.setPadding(new Insets(20, 2, 15, 20));
vbox1.setSpacing(20);
Button btnl2=new Button("SIGN IN");
btnl2.setFont(Font.font("Calibri",FontWeight.BOLD,16));
btnl2.setPrefSize(300,60);
btnl2.setStyle(" -fx-base: #0066cc;");
btnl2.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent e) {
signin();
}
});
Button btnl4=new Button("HELP");
btnl4.setFont(Font.font("Calibri",FontWeight.BOLD,16));
btnl4.setPrefSize(300,60);
btnl4.setStyle(" -fx-base: #0066cc;");
btnl4.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent e) {
help();
}
});
Button btnl5=new Button("ABOUT");
btnl5.setFont(Font.font("Calibri",FontWeight.BOLD,16));
btnl5.setPrefSize(300,60);
btnl5.setStyle(" -fx-base: #0066cc;");
btnl5.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent e) {
about();
}
});
Button btnl6=new Button("EXIT");
btnl6.setFont(Font.font("Calibri",FontWeight.BOLD,16));
btnl6.setPrefSize(300,60);
btnl6.setStyle(" -fx-base: #0066cc;");
btnl6.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent e) {
System.exit(0);
}
});
vbox1.getChildren().addAll(btnl2,btnl4,btnl5,btnl6);
return vbox1;
}
public static void main(String[] args) {
launch(args);
}
}
0(LauncherImpl.java:47)
at com.sun.javafx.application.LauncherImpl.run(LauncherImpl.java:115)
at java.lang.Thread.run(Thread.java:744)
Caused by: java.lang.NoSuchMethodException: welcomepage.WelcomePage.<init>()
at java.lang.Class.getConstructor0(Class.java:2810)
at java.lang.Class.getConstructor(Class.java:1718)
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:275)
... 3 more
Java Result: 1
My source code is very large, so I excluded the import statements. I will provide a part of my code below:
我的源代码非常大,所以我排除了导入语句。我将在下面提供我的代码的一部分:
##代码##采纳答案by Sotirios Delimanolis
@assylias, from the comments, is correct. Your class implicitly has package visibility, since you haven't provided an access modifier. The Java Language Specification says
@assylias,从评论来看,是正确的。您的类隐式具有包可见性,因为您没有提供访问修饰符。Java 语言规范说
the default constructor has the default access implied by no access modifier.
默认构造函数具有无访问修饰符隐含的默认访问权限。
So your default no-arg constructor given by the compiler has default access.
因此,编译器给出的默认无参数构造函数具有默认访问权限。
The Class#getConstructor()
method which com.sun.javafx.application.LauncherImpl.launchApplication1
uses
的Class#getConstructor()
方法,其com.sun.javafx.application.LauncherImpl.launchApplication1
用途
Returns a Constructor object that reflects the specified public constructor of the class represented by this Class object.
返回一个 Constructor 对象,该对象反映了此 Class 对象表示的类的指定公共构造函数。
But the constructor isn't public
and therefore isn't visible to the getConstructor(..)
method. So you get
但是构造函数不是public
,因此对getConstructor(..)
方法不可见。所以你得到
NoSuchMethodException - if a matching method is not found.
NoSuchMethodException - 如果找不到匹配的方法。
Provide a public
constructor yourself or make your class public
.
public
自己提供一个构造函数或创建你的类public
。