0

I was trying to set up JavaFX with Visual Studio Code. I referenced this great answer by Jose Pereda. However, even after the last step, it is not working. It is still showing:

Exception in thread "main" java.lang.reflect.InvocationTargetException
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:567)
    at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1051)
Caused by: java.lang.RuntimeException: Unable to construct Application instance: class com.Main
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:890)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:195)
    at java.base/java.lang.Thread.run(Thread.java:835)
Caused by: java.lang.NoSuchMethodException: com.Main.<init>()
    at java.base/java.lang.Class.getConstructor0(Class.java:3354)
    at java.base/java.lang.Class.getConstructor(Class.java:2156)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$8(LauncherImpl.java:801)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:455)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:389)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
    at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
    ... 1 more

What I am really bummed by, and what I could not fathom is this line:

Caused by: java.lang.NoSuchMethodException: com.Main.<init>()

My directory structure: Directory Structure

Could someone explain what this means and how to fix it?

And if you need anything else, please do reply. I am not sure what all to put up.

Jaideep Shekhar
  • 808
  • 2
  • 7
  • 21
  • Does your Main class have a no argument constructor? – Roger Lindsjö Aug 12 '19 at 15:43
  • I don't have any constructor defined for Main yet. Do I need one? If so, what is it supposed to do? – Jaideep Shekhar Aug 12 '19 at 15:45
  • If there is no other constructor, then there is an implicit empty one. – Roger Lindsjö Aug 12 '19 at 15:47
  • Is your class public? – Slaw Aug 12 '19 at 15:53
  • Oh wow, you are right! It's working now. Thanks so much! But I didn't see anywhere the class had to be public. Why is that? – Jaideep Shekhar Aug 12 '19 at 15:56
  • The documentation of javafx.application.Application mentions these requirements: the subclass must be public with a public, no-arg constructor. If you don't explicitly specify a constructor then an implicit no-arg constructor is provided. However, this "default constructor" will have the same visibility as the class. Since your class wasn't public, neither was the constructor. – Slaw Aug 12 '19 at 17:53
  • @Slaw This deserves a proper answer :) – José Pereda Aug 12 '19 at 20:12
  • @JoséPereda I had answered a similar question before, but lacked the time/resources to search for it at the time of commenting. – Slaw Aug 13 '19 at 05:18
  • Thanks @Slaw for the prompt response. Yes, my exact error has been answered there. But why did I not get that answer as I searched my error on StackOverflow? – Jaideep Shekhar Aug 13 '19 at 06:01
  • I find Stack Overflow's search to be... subpar. Often, if I want results from Stack Overflow, I use the following _Google_ query: `site:stackoverflow.com `. Also, the Q&A doesn't have many views or upvotes, so that may affect search results. – Slaw Aug 13 '19 at 20:18
  • I suppose I agree with that. :-p Thanks for the suggestion! – Jaideep Shekhar Aug 15 '19 at 09:51

0 Answers0