0

I am creating a GUI program for a objectoriented program that is going to find a way from a coordinate in the labyrint. the program itself works without the gui program, but when trying to add the GUI to the program i wont work. I am using Intellij and have java14 and javafx14. i have added the following VM options (--module-path

C:\Users\Simen\Documents\Informatikk\IN1010\javafx-sdk-14.0.1\lib
--add-modules
javafx.controls,javafx.fxml
--add-exports
javafx.graphics/com.sun.javafx.sg.prism=ALL-UNNAMED)

also added both the JDK and SDK lib for the project structure.

This is the exception i am recieving:

    Exception in Application constructor
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:564)
    at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1051)
Caused by: java.lang.RuntimeException: Unable to construct Application instance: class SkrivHei
    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:832)
Caused by: java.lang.NoSuchMethodException: SkrivHei.<init>()
    at java.base/java.lang.Class.getConstructor0(Class.java:3427)
    at java.base/java.lang.Class.getConstructor(Class.java:2165)
    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:391)
    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

This is the following code im using:

import javafx.application.Application;
import javafx.stage.Stage;
import javafx.stage.FileChooser;
import javafx.scene.layout.GridPane;
import java.io.File;
import javafx.scene.shape.Rectangle;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.input.MouseEvent;
import javafx.event.EventHandler;
import javafx.scene.control.Button;
import javafx.scene.layout.Pane;

class GUILabyrint extends Application{
    private Rute[][] rutenett;
    private Labyrint labyrint = null;
    private Rectangle[][] rutenettGUI;
    private Stage stage;

    public class LosningKnapp implements EventHandler<MouseEvent> {
        int kolonneGUI, radGUI;
        boolean [][] utvei = null;

        public LosningKnapp(int radGUI, int kolonneGUI, boolean[][] utvei) { // Trykk på losningknapp
            this.radGUI = radGUI;
            this.kolonneGUI = kolonneGUI;
            this.utvei = utvei;
        }

        public void handle(MouseEvent e) {
            resett();
            for(int i = 0; i < labyrint.rad; i++){
                for(int j = 0; j < labyrint.kolonne; j++) {
                    Color color = Color.GREEN;
                    if (utvei[i][j]){
                        rutenettGUI[j][i].setFill(color);
                    }
                }
            }
        }
    }

    public class Trykk implements EventHandler<MouseEvent> {

        int kolonneGUI, radGUI;

        Trykk(int radGUI, int kolonneGUI) {
            this.radGUI = radGUI;
            this.kolonneGUI = kolonneGUI;
        }
        @Override
        public void handle(MouseEvent e) {  // Håndterer trykk på rute
            Liste<String> liste = null;
            try {
                liste = labyrint.finnUtveiFra(kolonneGUI, radGUI);
            } catch (InterruptedException ex) {
                ex.printStackTrace();
            }
            Liste<boolean [][]> losningsliste = new NyLenkeliste<>();
            if (liste.stoerrelse() > 0){
                for (int i = 0 ; i < liste.stoerrelse() ; i ++) {
                    boolean [][] utvei = Labyrint.losningKonvert(liste.hent(i));
                    losningsliste.leggTil(utvei);
                    resett();
                }

                losningsViser(radGUI, kolonneGUI, losningsliste);
            }

            else {
                System.out.println("Ingen utveier.");
            }
        }
    }

    public void start(Stage primaryStage) {
        this.stage = primaryStage;
        FileChooser fil = new FileChooser();
        fil.setInitialDirectory(new File("."));
        File file = fil.showOpenDialog(stage);
        try {
            labyrint = Labyrint.lesFraFil(file);
        } catch (Exception e) {
            System.out.println("Fant ingen fil aa aapne");
        }
        rutenett = labyrint.hentRutenett();
        rutenettGUI = new Rectangle[labyrint.rad][labyrint.kolonne];
        GridPane labGrid = new GridPane();
        Rectangle rute;
        for(int i = 0; i < labyrint.rad; i++){
            for(int j = 0; j < labyrint.kolonne; j++) {
                Color color = rutenett[j][i] instanceof HvitRute ? Color.WHITE : Color.BLACK;
                rute = new Rectangle (20, 20, color);
                Button button = new Button("", rute);
                rutenettGUI[j][i] = rute;
                labGrid.add(button, i, j);
                Trykk trykk = new Trykk(i, j);
                button.setOnMouseClicked(trykk);
            }
        }
        Scene scene = new Scene(labGrid);
        stage.setScene(scene);
        stage.show();
    }

    public void resett() {
        for(int i = 0; i < labyrint.rad; i++){
            for(int j = 0; j < labyrint.kolonne; j++) {
                Color color = rutenett[j][i] instanceof HvitRute ? Color.WHITE : Color.BLACK;
                rutenettGUI[j][i].setFill(color);
            }
        }
    }

    public void losningsViser(int radGUI, int kolonneGUI, Liste<boolean[][]> losninger) {
        GridPane labGrid = new GridPane();
        Rectangle rute;
        for(int i = 0; i < labyrint.rad; i++){
            for(int j = 0; j < labyrint.kolonne; j++) {
                Color color = rutenett[j][i] instanceof HvitRute ? Color.WHITE : Color.BLACK;
                rute = new Rectangle (20, 20, color);
                Button button = new Button("", rute);
                rutenettGUI[j][i] = rute;
                labGrid.add(button, i, j);
                Trykk trykk = new Trykk(i, j);
                button.setOnMouseClicked(trykk);
            }
        }
        Pane kulisser = new Pane();
        kulisser.setPrefSize(500, 450);
        kulisser.getChildren().add(labGrid);
        int i = 1;
        int x = 20;
        for (boolean[][] losning : losninger) {
            Button losningsknapp = new Button("Losning " + i);
            losningsknapp.setLayoutX(x); losningsknapp.setLayoutY(400);
            LosningKnapp los = new LosningKnapp(radGUI, kolonneGUI, losning);
            losningsknapp.setOnMouseClicked(los);
            kulisser.getChildren().add(losningsknapp);
            x = x + 80;
            i ++;
        }

        Scene scene = new Scene(kulisser);
        stage.setScene(scene);
        stage.show();
    }

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

}
3
  • Either you changed the name of the class you posted after running it and generating the stack trace, or you didn't include the source of the issue. According to the stack trace, your SkrivHei class needs to be public, with a public, zero-argument constructor, and one of those conditions isn't being met. Commented May 12, 2020 at 15:03
  • yes i changed the class name so that is not the issue. what you proposed, solved the "exception in application constructur" however im now getting an "exception in application start method". Commented May 12, 2020 at 15:12
  • OK, good, so you have fixed the original issue. Commented May 12, 2020 at 15:12

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.