I'm having a problem with running my JavaFX sudoku app.
I have a method void updateBoard() that uses sudokuBoard object and assigns the numbers from textFields to every sudokuBoard field. One textField is responsible for one sudokuBoard field.
public void updateBoard() {
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
if ((Integer) Integer.parseInt(textFields[i][j].getText()) != null) {
sudokuBoard.set(i, j, Integer.parseInt(textFields[i][j].getText()));
} else {
sudokuBoard.set(i, j, null);
}
index++;
}
}
}
Then I also have a method boolean checkBoard() that checks if sudokuBoard is correctly solved.
public boolean checkBoard() {
updateBoard();
int counter = 0;
if (difficultyLevel.getLevel() == 0) {
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
if (counter % 5 == 0) {
if (sudokuBoard.getValue(i, j) == null) {
return false;
}
if (sudokuBoard.getValue(i, j) != difficultyLevel.getRejectedNumbers()[i][j]) {
return false;
}
}
counter++;
}
}
}
if (difficultyLevel.getLevel() == 1) {
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
if (counter % 3 == 0) {
if (sudokuBoard.getValue(i, j) == null) {
return false;
}
if (sudokuBoard.getValue(i, j) != difficultyLevel.getRejectedNumbers()[i][j]) {
return false;
}
}
counter++;
}
}
}
if (difficultyLevel.getLevel() == 2) {
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
if (counter % 2 == 0) {
if (sudokuBoard.getValue(i, j) == null) {
return false;
}
if (sudokuBoard.getValue(i, j) != difficultyLevel.getRejectedNumbers()[i][j]) {
return false;
}
}
counter++;
}
}
}
return true;
}
when I'm calling method updateBoard() from method checkBoard() it works perfectly as it should.
Hovewer when I'm calling this method (after pressing the button):
public void saveGameSudokuBoardButtonPushed() throws Exception {
updateBoard();
Stage stage = (Stage) saveGameSudokuBoardButton.getScene().getWindow();
FileChooser fileChooser = new FileChooser();
fileChooser.setTitle("Zapisz plik");
File file = fileChooser.showSaveDialog(stage);
FileSudokuBoardDao fileSudokuBoardDao = null;
try {
fileSudokuBoardDao = (FileSudokuBoardDao) SudokuBoardDaoFactory.getFileDao(file.toPath().toString());
} catch (NullPointerException exception) {
exception.printStackTrace();
}
fileSudokuBoardDao.write(sudokuBoard);
}
I get the following error:
Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at javafx.fxml/javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1787)
at javafx.fxml/javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1670)
at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at javafx.base/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
at javafx.base/javafx.event.Event.fireEvent(Event.java:198)
at javafx.graphics/javafx.scene.Node.fireEvent(Node.java:8879)
at javafx.controls/javafx.scene.control.Button.fire(Button.java:200)
at javafx.controls/com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:206)
at javafx.controls/com.sun.javafx.scene.control.inputmap.InputMap.handle(InputMap.java:274)
at javafx.base/com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at javafx.base/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.base/javafx.event.Event.fireEvent(Event.java:198)
at javafx.graphics/javafx.scene.Scene$MouseHandler.process(Scene.java:3851)
at javafx.graphics/javafx.scene.Scene$MouseHandler.access$1200(Scene.java:3579)
at javafx.graphics/javafx.scene.Scene.processMouseEvent(Scene.java:1849)
at javafx.graphics/javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2588)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:397)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$2(GlassViewEventHandler.java:434)
at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:390)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:433)
at javafx.graphics/com.sun.glass.ui.View.handleMouseEvent(View.java:556)
at javafx.graphics/com.sun.glass.ui.View.notifyMouse(View.java:942)
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)
at java.base/java.lang.Thread.run(Thread.java:832)
Caused by: 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 com.sun.javafx.reflect.Trampoline.invoke(MethodUtil.java:76)
at jdk.internal.reflect.GeneratedMethodAccessor2.invoke(Unknown Source)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at javafx.base/com.sun.javafx.reflect.MethodUtil.invoke(MethodUtil.java:273)
at javafx.fxml/com.sun.javafx.fxml.MethodHelper.invoke(MethodHelper.java:83)
at javafx.fxml/javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1784)
... 47 more
Caused by: java.lang.NumberFormatException: For input string: ""
at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:68)
at java.base/java.lang.Integer.parseInt(Integer.java:662)
at java.base/java.lang.Integer.parseInt(Integer.java:770)
at gui.SudokuBoardController.updateBoard(SudokuBoardController.java:86)
at gui.SudokuBoardController.saveGameSudokuBoardButtonPushed(SudokuBoardController.java:174)
... 58 more
Process finished with exit code 0
I don't know why the problem occurs while calling saveGameSudokuBoardButtonPushed() method and it do not occur while calling checkBoard() method.
method saveGameSudokuBoardButtonPushed() works perfectly when there is no method updateBoard() called inside it.
//EDIT//
I should add that my sudokuBoard has implemented diffuculty levels that remove certain fields from sudokuBoard. Those removed fields are saved in those difficulty level classes and in their places there is null in sudokuBoard field. Then sudokuBoard fields are assigned to textFields on GUI. So it is ok that when I'm trying to save the board that some fields are empty. However I'm wondering why it depends if I'm using updateBoard() method in checkBoard() method and saveGameSudokuBoardButtonPushed() if the implementation is the same for both of them
""
can not be parsed into an integer. Therefore, you need to skip parsing a blank string. You can do it as follows:
if (!textFields[i][j].getText().isBlank()) {
sudokuBoard.set(i, j, Integer.parseInt(textFields[i][j].getText()));
}