0

I want to program after the Model View Controller Concept in my JavaFX application and so I coded the launch method into my view package in the GUI class. Here you can see my GUI Class:

public class GUIFalscherName extends Application {


// Komplexe Datentypen
  //*********************************//
  VBox elternteil;

  Label testLabel;
  //*********************************//

  public GUIFalscherName(String[] args) {
    GUIFalscherName.launch(args);
  }

  public void start(Stage buehne) throws Exception {
    buehne.setTitle("Test");
    this.elternteil = new VBox();
    this.testLabel = new Label();
    this.testLabel.setText("test");
    fuegeKomponenteDemParentHinzu(testLabel);

    Scene scene = new Scene(elternteil);
    buehne.setScene(scene);
    buehne.show();
  }

  public void fuegeKomponenteDemParentHinzu(Node komponente) {
    this.elternteil.getChildren().add(komponente);
  }
}

Here you can see my main class from where I want to call the constructor of the GUI class where the launch is to launch the GUI.

public class FalscherName {
  public static void main(String[] args) {
    GUIFalscherName guiFalscherName = new GUIFalscherName(args);
  }
}

So my goal is to tear the main part apart from the GUI part and to not mix them, as the Model View Controller Concept says.

However, when I run my code I get the following error:

Exception in Application constructor
Exception in thread "main" java.lang.RuntimeException: Unable to construct Application instance: class de.falschername.view.GUIFalscherName
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:907)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$1(LauncherImpl.java:182)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NoSuchMethodException: de.falschername.view.GUIFalscherName.<init>()
    at java.lang.Class.getConstructor0(Unknown Source)
    at java.lang.Class.getConstructor(Unknown Source)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$7(LauncherImpl.java:818)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$7(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$5(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$6(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$4(WinApplication.java:186)
    ... 1 more

What's going wrong here?

CyanCoding
  • 1,012
  • 1
  • 12
  • 35
Xydru
  • 3
  • 3
  • Possible duplicate of [Unable to construct javafx.application.Application instance](https://stackoverflow.com/questions/25599423/unable-to-construct-javafx-application-application-instance). – CyanCoding Apr 11 '20 at 12:49

2 Answers2

0

This is the main problem:

Caused by: java.lang.NoSuchMethodException: de.falschername.view.GUIFalscherName.<init>()

Which is saying it can't find a no-argument constructor in the GUIFalscherName class. This is to be expected since you only have one constructor, and it's:

public GUIFalscherName(String[] args) {
    GUIFalscherName.launch(args);
}

Which expects a single argument. The need for a no-argument constructor is documented by Application:

The Application subclass must be declared public and must have a public no-argument constructor [emphasis added].

You placement of GUIFalscherName.launch(...) is also incorrect. When you launch a JavaFX application via launch, the JavaFX runtime will construct an instance of your application class for you (using reflection). Since you invoke launch in the constructor that means you're creating an instance which causes a different instance to be created, while the original instance is simply left to be garbage collected. If you want to launch JavaFX from a separate main class, one which doesn't inherit from Application, then you need to use Application#launch(Class,String...):

Launch a standalone application. This method is typically called from the main method. It must not be called more than once or an exception will be thrown.

The launch method does not return until the application has exited, either via a call to Platform.exit() or all of the application windows have been closed. The class specified by the appClass argument must be a public subclass of Application with a public no-argument constructor, in a package that is exported (or open) to at least the javafx.graphics module, or a RuntimeException will be thrown.

Note: The part dealing with exported/opened packages is related to modules which were added in Java 9. If you aren't developing in Java 9+, or otherwise not using modules, then that part is not relevant to you.

For example:

import javafx.application.Application;

public class FalscherName {

    public static void main(String[] args) {
        Application.launch(GUIFalscherName.class, args);
    }
}

And you need to remove the constructor you have defined in GUIFalscherName.

If you need access to the command line arguments, use Application#getParameters().

Slaw
  • 37,820
  • 8
  • 53
  • 80
-1

Please see below java doc.

Launch a standalone application. This method is typically called from the main method(). It must not be called more than once or an exception will be thrown. This is equivalent to launch(TheClass.class, args) where TheClass is the immediately enclosing class of the method that called launch. It must be a subclass of Application or a RuntimeException will be thrown.

When I try to call from other class then I get below error:

Exception in thread "main" java.lang.RuntimeException: Error: class FalcherName is not a subclass of javafx.application.Application
at javafx.application.Application.launch(Application.java:254)
at FalcherName.main(FalcherName.java:5)
Ravi Nain
  • 605
  • 8
  • 16