MineSweeper Projekt: Testen nicht möglich

ZH1896ZH

Mitglied
Hallo allerseits,
Bin gerade an einem Minesweeper-Projekt für die Uni dran. Wir müssen das Projekt nach dem MVC-Modell programmieren, dies führt dazu dass ich Probleme habe die MenuBar (im View) zu testen (Fehlermeldungen en mass). Kann mir jemand erklären, wieso das Programm nicht ordnungsgemäss läuft? ich reiss mir nämlich gleich den Kopf aus.

Hier die 4 Klassen:

Main:


Java:
package minesweeper;

import javafx.application.Application;
import javafx.stage.Stage;

public class MineSweeperMVC extends Application {

    protected MineSweeperModel model;
    protected MineSweeperController controller;
    protected MineSweeperView view;
    protected Stage stage;
    

    public void start(Stage primaryStage) throws Exception {
        model = new MineSweeperModel();
        view = new MineSweeperView(stage, model);
        controller = new MineSweeperController(model, view, stage);
        
        view.start();
    }
    
    
    
    public static void main(String[] args) {
        launch();

    }
}

Controller:

Java:
package minesweeper;

import javafx.application.Platform;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.ButtonType;
import javafx.stage.Stage;

public class MineSweeperController {
    
    protected MineSweeperModel model;
    protected MineSweeperView view;
    protected Stage stage;

    protected MineSweeperController(MineSweeperModel model, MineSweeperView view, Stage stage) {
        this.model = model;
        this.view = view;
        this.stage = stage;
        
        //ActionEvents from MenuBar
        view.aboutItem.setOnAction(e -> {
            Alert aboutAlert = new Alert(AlertType.INFORMATION, "Created by Robin Roth, Robin Weis and Luca Schädler\n" + "v 0.1", ButtonType.CLOSE);// write the text
            aboutAlert.setTitle("About");
            aboutAlert.setHeaderText("MineSweeper");
            aboutAlert.showAndWait();
        });
        
        view.helpItem.setOnAction(e -> {
            Alert helpAlert = new Alert(AlertType.INFORMATION,
                    "The aim of minesweeper is to identify all the sqaures which contain mines.\n\n"
                            + "Left click on a square to reveal a number. This number indicates how many of the adjacent squares contain mines. By using these numbers you can deduce which sqaures contain mines. \n\n"
                            + "Right click on a square to mark it as containing a mine. You can right click the sqaure again to unmark it if you made a mistake.\n\n"
                            + "After all mines have successfully been marked the game is over and you win! Be careful though. Left clicking a square with a mine will result in a game over.", ButtonType.CLOSE);// write the text
            helpAlert.setTitle("Help");
            helpAlert.setHeaderText("How to play");
            helpAlert.showAndWait();
        });
        
        view.quitItem.setOnAction(e -> {
            Platform.exit();
        });
        
        view.smallSizeItem.setOnAction(e -> {
            MineSweeperView.gridSize = 10;
            model.reload();
        });
        
        view.smallSizeItem.setOnAction(e -> {
            MineSweeperView.gridSize = 15;
            model.reload();
        });

        view.smallSizeItem.setOnAction(e -> {
            MineSweeperView.gridSize = 20;
            model.reload();
        });
        
        view.easyItem.setOnAction(e -> {
            MineSweeperView.bombPercent = 10;
            model.reload();
        });
        
        //view.normalItem.setOnAction(e -> addBomb());//kann so mit event get source gemacht werden noch machen
        
        view.hardItem.setOnAction(e -> {
            MineSweeperView.bombPercent = 20;
            model.reload();
        });
        
        view.soundOnItem.setOnAction(e -> {
            MineSweeperView.sound = true;
        });
        
        view.soundOffItem.setOnAction(e -> {
            MineSweeperView.sound = false;
        });
        
    }

}

View:

Java:
package minesweeper;

import java.util.Timer;

import javafx.scene.Scene;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;
import javafx.scene.control.RadioMenuItem;
import javafx.scene.control.ToggleGroup;
import javafx.scene.image.Image;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class MineSweeperView {

    protected Stage primaryStage;
    protected MineSweeperModel model;
    
    public static int gridSize = 10;
    public static int bombPercent = 10;
    public static boolean sound = true;
    public static Timer timer;
    public static int secondPassed;

    private VBox root = new VBox();
    protected MenuBar menuBar;
    protected Menu fileMenu, sizeMenu, difficultyMenu, soundMenu;
    protected MenuItem aboutItem, helpItem, quitItem,
                    smallSizeItem, mediumSizeItem, largeSizeItem,
                    easyItem, normalItem, hardItem;
    protected RadioMenuItem soundOnItem, soundOffItem;

    protected MineSweeperView(Stage primaryStage, MineSweeperModel model) {
        this.primaryStage = primaryStage;
        this.model = model;
        
        //Menu-Instanziierung
        menuBar = new MenuBar();
        fileMenu = new Menu("File"); sizeMenu = new Menu("Size"); difficultyMenu = new Menu("Difficulty");soundMenu = new Menu("Sound");
        aboutItem = new MenuItem("About"); helpItem = new MenuItem("help"); quitItem = new MenuItem("Quit");
        smallSizeItem = new MenuItem("10x10 (small)"); mediumSizeItem = new MenuItem("15x15 (medium)"); largeSizeItem = new MenuItem("20x20 (large)");
        easyItem = new MenuItem("Easy"); normalItem = new MenuItem("Normal"); hardItem = new MenuItem("Hard");
        soundOnItem = new RadioMenuItem("On"); soundOffItem = new RadioMenuItem("Off");
        
        fileMenu.getItems().addAll(aboutItem, helpItem, quitItem);
        sizeMenu.getItems().addAll(smallSizeItem, mediumSizeItem, largeSizeItem);
        difficultyMenu.getItems().addAll(easyItem, normalItem, hardItem);
        soundMenu.getItems().addAll(soundOnItem, soundOffItem);
        
        ToggleGroup soundToggle = new ToggleGroup();
        soundToggle.getToggles().addAll(soundOnItem, soundOffItem);
        soundToggle.selectToggle(soundOnItem);
        
        menuBar.getMenus().addAll(fileMenu,sizeMenu, difficultyMenu, soundMenu);
        
        root.getChildren().add(menuBar);
        
        //Szene instanziieren und an Stage weitergeben
        Scene scene = new Scene(root);
        scene.getStylesheets().add(getClass().getResource("MineSweeperStyle.css").toExternalForm());
        primaryStage.setScene(scene);
        primaryStage.setTitle("MineSweeper");
    }

    public void start() {//change to procteted after testing
        primaryStage.show();
    }

}

Model:


Java:
package minesweeper;

public class MineSweeperModel {
    

    
    public void reload() {//methode to reload after userklick
        
    }

}


Fehlermeldung:

Exception in Application start method
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
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(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.launcher.LauncherHelper$FXHelper.main(Unknown Source)
Caused by: java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$1(LauncherImpl.java:182)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NullPointerException
at minesweeper.MineSweeperView.<init>(MineSweeperView.java:83)
at minesweeper.MineSweeperMVC.start(MineSweeperMVC.java:16)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$8(LauncherImpl.java:863)
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
Exception running application minesweeper.MineSweeperMVC
 
Dann wird mit 99,9 %-iger Wahrscheinlichkeit die angegebene Ressource nicht gefunden. Gib mal das Paket als Pfad mit an, wo sich die CSS-Datei befindet. Je nach Build-System musst Du die Ressource auch in einem anderen Verzeichnisbaum (src/main/resources) ablegen.
 
Dann wird mit 99,9 %-iger Wahrscheinlichkeit die angegebene Ressource nicht gefunden. Gib mal das Paket als Pfad mit an, wo sich die CSS-Datei befindet. Je nach Build-System musst Du die Ressource auch in einem anderen Verzeichnisbaum (src/main/resources) ablegen.
Du hast recht, wenn ich die Zeile auskommentiere funktioniert das Programm.
Kannst du mir noch sagen, wie ich den Pfad angeben muss? (siehe Screenshot)


1584476289785.png
 
Probier mal scene.getStylesheets().add(getClass().getResource("/MineSweeperStyle.css").toExternalForm());, also mit führendem Slash.
hab bisschen herumprobiert, so funktionierts:
Code:
scene.getStylesheets().add(getClass().getResource("/resources/MineSweeperStyle.css").toExternalForm());
das "/" - Zeichen am anfang hat gefehlt. Danke für Deine Hilfe mihe7!
 

Neue Themen


Zurück
Oben