0

Trying to create an application with javaFX. this class extends Application. The error message leads me to believe that there is something wrong with my subclasses constructor, yet it calls super (Application) with all the relevant params (none).

public class SameGame extends Application {
private Button[][] board;
public Button[][] getBoard() {
    return board;
}
public SameGame(int BoardSize){
    super();
    board = new Button[BoardSize][BoardSize]; //only make squares
    for (int x = 0; x < getBoard().length; x++) {
        for (int y = 0; y < getBoard()[x].length; y++) {
            board[x][y] = new Button();
        }
    }
}
public void start(Stage primaryStage) {
    GridPane gridpane = new GridPane();
    Scene scene = new Scene(gridpane);
    primaryStage.setScene(scene);
    primaryStage.show();
    SameGame sameGame = new SameGame(5);

    for (int x = 0; x < getBoard().length; x++) {
        for (int y = 0; y < getBoard()[x].length; y++) {
            gridpane.add(getBoard()[x][y], x, y);
        }
    }
}

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

and heres the exception

Exception in Application constructor
java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
    at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Unable to construct Application instance: class SameGame
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:907)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$154(LauncherImpl.java:182)
    at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.NoSuchMethodException: SameGame.<init>()
    at java.lang.Class.getConstructor0(Class.java:3082)
    at java.lang.Class.getConstructor(Class.java:1825)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$160(LauncherImpl.java:818)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$174(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(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$147(WinApplication.java:177)
    ... 1 more
Exception running application SameGame

Process finished with exit code 1

i know the answer's in there, i just dont know enough to find it

  • 1
    How is the program expected to select the right value of `BoardSize` when it starts? – Dawood ibn Kareem Dec 03 '17 at 23:22
  • mmm that should probably come from command line args... – Colin m Gilker Dec 03 '17 at 23:23
  • The `launch()` method creates an instance of the `Application` class by calling its no-arg constructor (via reflection). Since you don't have a no-arg constructor, you get an error. As @DawoodibnKareem points out, your current code doesn't make much sense anyway... – James_D Dec 03 '17 at 23:25
  • 2
    Maybe it could come from the command line, yes. But for now, your application needs to have a constructor `public SameGame()` for it to run when it starts. It could be as simple as `public SameGame() { this(5); }` for example, which calls the other constructor. – Dawood ibn Kareem Dec 03 '17 at 23:25

1 Answers1

0

got it! the constructor for same game shouldnt have any arguments and the input for BoardSize should come from cmd args when the program is run

public SameGame(){
        int BoardSize = Integer.parseInt(this.getParameters().getRaw().get(0));

        board = new Button[BoardSize][BoardSize]; //get size of board fro cmd args
        for (int x = 0; x < getBoard().length; x++) {
            for (int y = 0; y < getBoard()[x].length; y++) {
                board[x][y] = new Button();
            }
        }

    }