繁体   English   中英

为什么显示“ java.lang.reflect.InvocationTargetException”

[英]Why “java.lang.reflect.InvocationTargetException” showing

我正在使用NetBeans IDEjavafx开发一个项目。 每当我运行代码时,都会遇到一些异常。 直到昨天都没有问题。 例外情况如下:

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$000(LauncherImpl.java:47)
    at com.sun.javafx.application.LauncherImpl$1.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

我的源代码非常大,因此排除了import语句。 我将在下面提供部分代码:

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

@assylias,从评论中是正确的。 由于您未提供访问修饰符,因此您的类隐式具有包可见性。 Java语言规范说

默认构造函数具有无访问修饰符暗含的默认访问权限。

因此,编译器提供的默认无参数构造函数具有默认访问权限。

com.sun.javafx.application.LauncherImpl.launchApplication1使用的Class#getConstructor()方法

返回一个构造函数对象,该对象反映此Class对象表示的类的指定公共构造函数。

但是构造函数不是public ,因此对getConstructor(..)方法不可见。 所以你得到

NoSuchMethodException-如果找不到匹配的方法。

自己提供一个public构造函数或public您的班级。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM