JTDotz
JTDotz

Reputation: 63

Extending an extension of Application throws InvocationTargetException

I am working on a project that will have a lot of JavaFX applications with similar, but different enough functionality, so I made an abstract base class that extends Application to handle the common functionality and dictate what they need to do, and a bunch of concrete classes that extend that one. However, when I try to run, I get

Exception in Application constructor 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:564) at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:473) at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:372) 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:564) at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:941) Caused by: java.lang.RuntimeException: Unable to construct Application instance: class toolBase.Tool at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:963) at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:198) at java.base/java.lang.Thread.run(Thread.java:844) Caused by: java.lang.InstantiationException at java.base/jdk.internal.reflect.InstantiationExceptionConstructorAccessorImpl.newInstance(InstantiationExceptionConstructorAccessorImpl.java:48) at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:488) at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$8(LauncherImpl.java:875) at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$11(PlatformImpl.java:449) at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$9(PlatformImpl.java:418) at java.base/java.security.AccessController.doPrivileged(Native Method) at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:417) 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:175) ... 1 more Exception running application ccs.CCS

This is the gist of the base class:

public abstract class Tool extends Application {
public CCS ccs;
public abstract String getLongName();
public abstract String getShortName();
public abstract String getVersion();

public void start(Stage primaryStage) {
    try {
        primaryStage.setTitle("[Company] " + getLongName() + " v" + getVersion());
        Scene scene = new Scene(build(primaryStage), 200, 100);
        primaryStage.setScene(scene);
        primaryStage.show();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
public abstract Parent build(Stage stage);

public static void main(String[] args) {
    launch(args);
}

@Override public void stop() { ccs.openTools.remove(getLongName()); }
}  

And all of the extensions look like this right now:

public class ACT extends Tool {
public String getLongName() {return "[Tool name]"; }
public String getShortName() {return "ACT"; }
public String getVersion() {return "α.0";}

public Parent build(Stage stage) {
    BorderPane root = new BorderPane();

    Button b = new Button("Do "+getShortName()+" stuff");
    b.setOnMouseClicked((e) -> {
        System.out.println(getShortName()+" stuff");
    });
    root.setCenter(b);
    return root;
}
}

All the dependencies and everything are fine. When I instead make them extend Application and copy the common functionality from the base class, they run perfectly. Does anyone know what's going on?

Upvotes: 1

Views: 1460

Answers (1)

Jai
Jai

Reputation: 8363

You should not have the public static void main(String[] args) in the base class. Each extending class needs to include its own copy of that same method.

Doing that in base class causes JavaFX to try to launch a copy of Tool class, which obviously wouldn't work because it is abstract.

Upvotes: 3

Related Questions