Hallo,
Bin wiedermal auf ein Problem gestossen beim MineSweeper-Projekt. Es geht um das Erstellen des Spielfelds. Die zwei Methoden (reload und createContent) sind dafür da. Es wird eine neue Pane erstellt und diese mit Buttons, etc. befüllt. Die Methode reload (welche createContent aufruft) wird beim Start des Programmes, sowie bei Benutzerinteraktionen (Spielfeldgrösse wird verändert == neues Spiel) aufgerufen. Jetzt folgender Sachverhalt: Programm startet nicht mehr korrekt auf (siehe Stack Trace), ich habe nun die Methode createContent() durch createContenttt() ausgewechselt, um zu testen woran es liegene könnte. Das Programm startet nun auf und wenn die Spielfeldgrösse verändert wird über die MenuBar funktioniert die Methode createContent() wunschgemäss. Nur beim Start scheint irgendetwas nicht zu funktionieren. Wir zerbrechen uns schon den ganzen Tag den Kopf daran...hoffe jemand aus dem Forum kann uns einen Gedankenanstoss geben.
Anbei die Code-Auszüge:
Methoden:
- reload():
- createContent():
- createContenttt(): (testmethode!)
Ausschnitt aus Controller:
Zeilen aus Stack Trace:
- Model Z62
- View Z76:
- MVC Z15:
Beste Grüsse
ZH1896ZH
Bin wiedermal auf ein Problem gestossen beim MineSweeper-Projekt. Es geht um das Erstellen des Spielfelds. Die zwei Methoden (reload und createContent) sind dafür da. Es wird eine neue Pane erstellt und diese mit Buttons, etc. befüllt. Die Methode reload (welche createContent aufruft) wird beim Start des Programmes, sowie bei Benutzerinteraktionen (Spielfeldgrösse wird verändert == neues Spiel) aufgerufen. Jetzt folgender Sachverhalt: Programm startet nicht mehr korrekt auf (siehe Stack Trace), ich habe nun die Methode createContent() durch createContenttt() ausgewechselt, um zu testen woran es liegene könnte. Das Programm startet nun auf und wenn die Spielfeldgrösse verändert wird über die MenuBar funktioniert die Methode createContent() wunschgemäss. Nur beim Start scheint irgendetwas nicht zu funktionieren. Wir zerbrechen uns schon den ganzen Tag den Kopf daran...hoffe jemand aus dem Forum kann uns einen Gedankenanstoss geben.
Anbei die Code-Auszüge:
Methoden:
- reload():
Java:
public static void reload() {//methode works LS
MineSweeperView.grid = new StackButton[MineSweeperView.gridSize][MineSweeperView.gridSize];
MineSweeperView.secondsPassed = 0;
TimerTask task = new TimerTask() {
@Override
public void run() {
MineSweeperView.secondsPassed++;
};
};
MineSweeperView.timer.cancel();
MineSweeperView.timer = new Timer();
MineSweeperView.timer.schedule(task, 1000, 1000);
MineSweeperView.root.getChildren().remove(1);
MineSweeperView.root.getChildren().add(createContent());
MineSweeperView.primaryStage.sizeToScene();
}
Java:
protected static Parent createContent() { //idk why method doesnt work (LS)
// Reset in case of a new game
MineSweeperView.numBombs = 0;
MineSweeperView.foundBombs = 0;
Pane secondroot = new Pane();
secondroot.setPrefSize(MineSweeperView.gridSize * 40, MineSweeperView.gridSize * 40);
// Create the StackButton and Bombs on percentage
for (int i = 0; i < MineSweeperView.gridSize; i++) {
for (int j = 0; j < MineSweeperView.gridSize; j++) {
StackButton stackButton = new StackButton(j, i, Math.random() < (double) MineSweeperView.bombPercent);
MineSweeperView.grid[j][i] = stackButton;
secondroot.getChildren().add(stackButton);
}
}
// add the value under the buttons and the color
for (int i = 0; i < MineSweeperView.gridSize; i++) {
for (int j = 0; j < MineSweeperView.gridSize; j++) {
int bombNeighbours = 0;
ArrayList<StackButton> neighbours = new ArrayList<StackButton>();
int[] locs = new int[] { -1, -1, -1, 0, -1, 1, 0, -1, 0, 1, 1, -1, 1, 0, 1, 1 };
for (int l = 0; l < locs.length; l++) {
int dx = locs[l];
int dy = locs[++l];
int newX = dx + j;
int newY = dy + i;
if (newX >= 0 && newX < MineSweeperView.gridSize && newY >= 0 && newY < MineSweeperView.gridSize) {
neighbours.add(MineSweeperView.grid[newX][newY]);
if (MineSweeperView.grid[newX][newY].hasBomb) {
bombNeighbours++;
}
}
}
MineSweeperView.grid[j][i].numBombs = bombNeighbours;
MineSweeperView.grid[j][i].neighbours = neighbours;
Color[] colors = { null, Color.BLUE, Color.GREEN, Color.RED, Color.DARKBLUE, Color.DARKRED, Color.CYAN,
Color.BLACK, Color.DARKGRAY };
MineSweeperView.grid[j][i].color = colors[MineSweeperView.grid[j][i].numBombs];
}
}
return secondroot;
}
Java:
public static Pane createContenttt() {//testmethod works (LS)
Pane roott = new Pane();
roott.getChildren().add(new Button("Hallo"));
return roott;
}
}
Java:
view.smallSizeItem.setOnAction(e -> {
MineSweeperView.gridSize = 10;
model.reload();
});
view.mediumSizeItem.setOnAction(e -> {
MineSweeperView.gridSize = 15;
model.reload();
});
view.largeSizeItem.setOnAction(e -> {
MineSweeperView.gridSize = 20;
model.reload();
});
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.MineSweeperModel.createContent(MineSweeperModel.java:62)
at minesweeper.MineSweeperView.<init>(MineSweeperView.java:76)
at minesweeper.MineSweeperMVC.start(MineSweeperMVC.java:15)
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
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.MineSweeperModel.createContent(MineSweeperModel.java:62)
at minesweeper.MineSweeperView.<init>(MineSweeperView.java:76)
at minesweeper.MineSweeperMVC.start(MineSweeperMVC.java:15)
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
Zeilen aus Stack Trace:
- Model Z62
Java:
// Create the StackButton and Bombs on percentage
for (int i = 0; i < MineSweeperView.gridSize; i++) {
for (int j = 0; j < MineSweeperView.gridSize; j++) {
StackButton stackButton = new StackButton(j, i, Math.random() < (double) MineSweeperView.bombPercent);
MineSweeperView.grid[j][i] = stackButton; //Zeile 62!!
secondroot.getChildren().add(stackButton);
}
}
- View Z76:
Java:
// MenuBar und Buttons werden der VBox(root) hinzugefügt
root.getChildren().addAll(menuBar, model.createContent());//createContent funktioniert hier noch nicht nur meine testmethode
- MVC Z15:
Java:
view = new MineSweeperView(primaryStage, model);
Beste Grüsse
ZH1896ZH