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

时间:2014-02-12 15:55:13

标签: java javafx javafx-2

我正在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$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);
 }
 }

1 个答案:

答案 0 :(得分:4)

来自评论的

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

  

默认构造函数具有默认访问权限隐含的默认访问权限。

因此,编译器给出的默认no-arg构造函数具有默认访问权限。

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

  

返回反映指定public的Constructor对象   此Class对象表示的类的构造函数。

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

  

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

自己提供public构造函数或创建类public