TomJ
TomJ

Reputation: 1813

Why "java.lang.reflect.InvocationTargetException" showing

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 :

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

My source code is very large, so I excluded the import statements. I will provide a part of my code below:

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

Upvotes: 2

Views: 35397

Answers (1)

Sotirios Delimanolis
Sotirios Delimanolis

Reputation: 279960

@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

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

Returns a Constructor object that reflects the specified public constructor of the class represented by this Class object.

But the constructor isn't public and therefore isn't visible to the getConstructor(..) method. So you get

NoSuchMethodException - if a matching method is not found.

Provide a public constructor yourself or make your class public.

Upvotes: 4

Related Questions