-2

Other possible solutions I got in different forums are not working. Here is my code.

class javaFX extends Application{

    @Override
    public void start(Stage primaryStage) throws Exception{
        primaryStage.setTitle("JavaFX app");
        Label label = new Label("Hello World, JavaFX !");
        Scene scene = new Scene(label, 400, 200);
        primaryStage.setScene(scene);
    
        primaryStage.show();
    }
    public static void main(String args[]) {
        Application.launch(args);
    }
}

This is the StackTrace. It is showing NoSuchElementException which is a bit weird.

Exception in Application constructor
Exception in thread "main" 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 sun.launcher.LauncherHelper$FXHelper.main(Unknown Source)
Caused by: java.lang.RuntimeException: Unable to construct Application instance: class xlSheets.javaFX
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:907)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$159(LauncherImpl.java:182)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NoSuchMethodException: xlSheets.javaFX.<init>()
    at java.lang.Class.getConstructor0(Unknown Source)
    at java.lang.Class.getConstructor(Unknown Source)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$165(LauncherImpl.java:818)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$179(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$177(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$178(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$152(WinApplication.java:177)
    ... 1 more
  • 4
    Your `Application` subclass needs to be `public`. OT: please follow proper Java naming conventions. (Also, the stack trace is showing a `NoSuchMethodException`, not a `NoSuchElementException`.) – James_D Jul 23 '20 at 18:18
  • ohh .. __DO NOT__ repost a question, instead edit the first to make it answerable, at the very least __LEARN__ from the comments (you still violate java naming conventions, stop that _immediately_!!) – kleopatra Jul 23 '20 at 22:18

1 Answers1

2

The Application.launch() method uses reflection to create an instance of the Application subclass, calling its constructor taking no arguments.

According to the documentation the Appplication subclass

must be a public subclass of Application with a public no-argument constructor

So in order for this to work, both the Application subclass and the constructor (if explicitly defined) need to be declared public.

The following fixes the problem (I also changed the class name to conform to Java naming conventions):

public class JavaFX extends Application{

    @Override
    public void start(Stage primaryStage) throws Exception{
        primaryStage.setTitle("JavaFX app");
        Label label = new Label("Hello World, JavaFX !");
        Scene scene = new Scene(label, 400, 200);
        primaryStage.setScene(scene);
    
        primaryStage.show();
    }
    public static void main(String args[]) {
        Application.launch(args);
    }
}
James_D
  • 201,275
  • 16
  • 291
  • 322
  • what does it mean by reflection, some other forums said the same things but I couldn't get it. – Kaustuva Kumar Sahu Jul 23 '20 at 18:25
  • @KaustuvaKumarSahu https://stackoverflow.com/questions/37628/what-is-reflection-and-why-is-it-useful – James_D Jul 23 '20 at 18:26
  • _stuff which i couldn't understand_ then it's time for learning java/fx language basics ... you find this most simple App code in literally __every__ introductory tutorial on fx, this site is not the place to repeat it, normally ;) – kleopatra Jul 23 '20 at 22:07
  • Ok ,i get it but why is changing the class to public helping it. – Kaustuva Kumar Sahu Jul 24 '20 at 07:15
  • @KaustuvaKumarSahu The underlying reason is that `Application.launch()` calls [this method](https://docs.oracle.com/en/java/javase/14/docs/api/java.base/java/lang/Class.html#getConstructor(java.lang.Class...)), which only finds publicly-accessible constructors. However, you don't need to dig into those details, simply read the documentation for the method you called (i.e. `Application.launch()`) that is throwing the exception. See edit. – James_D Jul 24 '20 at 12:58